Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #24505 > unrolled thread

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

Started byJean Dupont <jeandupont115@gmail.com>
First post2012-06-27 05:24 -0700
Last post2012-06-27 07:09 -0700
Articles 4 — 4 participants

Back to article view | Back to comp.lang.python


Contents

  [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

#24505 — [newbie] problem with data (different behaviour between batch and interactive use)

FromJean Dupont <jeandupont115@gmail.com>
Date2012-06-27 05:24 -0700
Subject[newbie] problem with data (different behaviour between batch and interactive use)
Message-ID<aec77bb0-7d2f-43f7-a556-3a59ad016112@googlegroups.com>
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

[toc] | [next] | [standalone]


#24509

FromJussi Piitulainen <jpiitula@ling.helsinki.fi>
Date2012-06-27 15:46 +0300
Message-ID<qotsjdgvs5s.fsf@ruuvi.it.helsinki.fi>
In reply to#24505
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)

[toc] | [prev] | [next] | [standalone]


#24511

FromCousin Stanley <cousinstanley@gmail.com>
Date2012-06-27 13:23 +0000
Message-ID<jsf1gl$gld$1@dont-email.me>
In reply to#24505
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

[toc] | [prev] | [next] | [standalone]


#24516

FromJustin Barber <barber.justin@gmail.com>
Date2012-06-27 07:09 -0700
Message-ID<a8dbb7f4-59ba-401e-99d8-10cd5a029fe4@googlegroups.com>
In reply to#24505
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

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web