Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #24509
| From | Jussi Piitulainen <jpiitula@ling.helsinki.fi> |
|---|---|
| Newsgroups | comp.lang.python |
| Subject | Re: [newbie] problem with data (different behaviour between batch and interactive use) |
| Date | 2012-06-27 15:46 +0300 |
| Organization | University of Helsinki |
| Message-ID | <qotsjdgvs5s.fsf@ruuvi.it.helsinki.fi> (permalink) |
| References | <aec77bb0-7d2f-43f7-a556-3a59ad016112@googlegroups.com> |
Jean Dupont writes:
> If I start python interactively I can separate the fields as
> follows:
> >measurement=+3.874693E01,+9.999889E03,+9.910000E+37,+1.876[...]
> >print measurement[0]
> 0.3874693
[...]
> The script does this:
> measurement=serkeith.readline().replace('\x11','').replace([...]
> print measurement[0]
> +
[...]
> can anyone here tell me what I'm doing wrong and how to do it
> correctly
When you index a string, you get characters. Your script handles a
line as a string. Interact with Python using a string for your data to
learn how it behaves and what to do: split the string into a list of
written forms of the numbers as strings, then convert that into a list
of those numbers, and index the list.
This way:
>>> measurement = "+3.874693E01,+9.999889E03,+9.910000E+37"
>>> measurement
'+3.874693E01,+9.999889E03,+9.910000E+37'
>>> measurement.split(',')
['+3.874693E01', '+9.999889E03', '+9.910000E+37']
>>> measurement.split(',')[0]
'+3.874693E01'
>>> float(measurement.split(',')[0])
38.746929999999999
>>> map(float, measurement.split(','))
[38.746929999999999, 9999.8889999999992, 9.9100000000000005e+37]
>>> map(float, measurement.split(','))[0]
38.746929999999999
>>>
In your previous interactive session you created a tuple of numbers,
which is as good as a list in this context. The comma does that,
parentheses not required:
>>> measurement = +3.874693E01,+9.999889E03,+9.910000E+37
>>> measurement
(38.746929999999999, 9999.8889999999992, 9.9100000000000005e+37)
Back to comp.lang.python | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
[newbie] problem with data (different behaviour between batch and interactive use) Jean Dupont <jeandupont115@gmail.com> - 2012-06-27 05:24 -0700 Re: [newbie] problem with data (different behaviour between batch and interactive use) Jussi Piitulainen <jpiitula@ling.helsinki.fi> - 2012-06-27 15:46 +0300 Re: [newbie] problem with data (different behaviour between batch and interactive use) Cousin Stanley <cousinstanley@gmail.com> - 2012-06-27 13:23 +0000 Re: [newbie] problem with data (different behaviour between batch and interactive use) Justin Barber <barber.justin@gmail.com> - 2012-06-27 07:09 -0700
csiph-web