[newbie] problem with data (different behaviour between batch andinteractive use)

J

Jean Dupont

I have some data which is presented in the following format to me:
+3.874693E-01,+9.999889E-03,+9.910000E+37,+1.876595E+04,+3.994000E+04
I'm only interested in the first two fields i.e.
+3.874693E-01,+9.999889E-03
If I start python interactively I can separate the fields as follows:
measurement=+3.874693E01,+9.999889E03,+9.910000E+37,+1.876595E+04,+3.994000E+04
print measurement[0] 0.3874693
print measurement[1]
0.009999889
If however I run a script with the same commands I get something different:
The script does this:
measurement=serkeith.readline().replace('\x11','').replace('\x13','').replace('\x0d','\n')
print measurement[0]
+
print measurement[1]
3

can anyone here tell me what I'm doing wrong and how to do it correctly

thanks
jean
 
J

Jussi Piitulainen

Jean said:
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:
(38.746929999999999, 9999.8889999999992, 9.9100000000000005e+37)
 
C

Cousin Stanley

Jean said:
I have some data which is presented
in the following format to me :

+3.874693E-01,+9.999889E-03,+9.910000E+37,+1.876595E+04,+3.994000E+04

I'm only interested in the first two fields i.e.

+3.874693E-01,+9.999889E-03
....

The following program will read lines
of comma-separated data from a text file
and add each line as a row in a list of lists ....

The first two items in each row
could be accessed by their indexes ....

# --------------------------------------------------

#!/usr/bin/env python

fsource = open( 'edata.txt' )

ltarget = [ ]

for this_line in fsource :

this_list = this_line.strip().split( ',' )

that_list = [ float( x ) for x in this_list ]

ltarget.append( that_list )


for this_row in ltarget :

print ' %e' % this_row[ 0 ]
print ' %e' % this_row[ 1 ]
print

fsource.close()


# -----------------------------------------------------
#
# edata.txt

+3.874693E01,+9.999889E03,+9.910000E+37,+1.876595E+04,+3.994000E+04
1e01,2e02,3e03,4e04,5e05
5e-05,4e-04,3e-03,2e-02,1e-01
 
J

Justin Barber

When you are reading it in measurement is a string. The indicies of the string are going to be returned in your print statements.


Similar to having done this in the interpreter:
In [17]: measurement = '+3.874693E01,+9.999889E03,+9.910000E+37,+1.876595E+04,+3.994000E+04'

In [18]: measurement[1]
Out[18]: '3'

In [19]: measurement[0]
Out[19]: '+'

You need to split up your string and convert to floats.

measurement = map(float, serkeith.readline().replace('\x11','').replace('\x13','').replace('\x0d','\n').split(','))

Something like that should work...

to test in interpreter do the following :

measurement = map(float, '+3.874693E01,+9.999889E03,+9.910000E+37,+1.876595E+04,+3.994000E+04'.split(','))
In [24]: measurement[0]
Out[24]: 38.74693
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,904
Latest member
HealthyVisionsCBDPrice

Latest Threads

Top