Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > [newbie] problem with data (different behaviour between batch andinteractive use)

Reply
Thread Tools

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

 
 
Jean Dupont
Guest
Posts: n/a
 
      06-27-2012
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
 
Reply With Quote
 
 
 
 
Jussi Piitulainen
Guest
Posts: n/a
 
      06-27-2012
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)
 
Reply With Quote
 
 
 
 
Cousin Stanley
Guest
Posts: n/a
 
      06-27-2012
Jean Dupont wrote:

> 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


--
Stanley C. Kitching
Human Being
Phoenix, Arizona

 
Reply With Quote
 
Justin Barber
Guest
Posts: n/a
 
      06-27-2012
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.876595 E+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('\x 13','').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.876595 E+04,+3.994000E+04'.split(','))
In [24]: measurement[0]
Out[24]: 38.74693
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Funny behaviour with __future__ and doctest between 2.6 and 3.1 Mattsteel Python 4 01-30-2010 10:25 AM
Header behaviour between browsers Syl HTML 3 08-17-2007 03:10 PM
debugger behaviour different to execution behaviour Andy Chambers Java 1 05-14-2007 09:51 AM
Batch Updating EXIF Data John Ortt Digital Photography 11 04-26-2007 11:41 AM
problem with writing a batch file with foreign characters rachel cox Java 1 02-03-2004 08:30 AM



Advertisments