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

Discussion in 'Python' started by Jean Dupont, Jun 27, 2012.

  1. Jean Dupont

    Jean Dupont Guest

    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
    Jean Dupont, Jun 27, 2012
    #1
    1. Advertising

  2. Re: [newbie] problem with data (different behaviour between batch and interactive use)

    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)
    Jussi Piitulainen, Jun 27, 2012
    #2
    1. Advertising

  3. Re: [newbie] problem with data (different behaviour between batch and interactive use)

    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
    Cousin Stanley, Jun 27, 2012
    #3
  4. 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
    Justin Barber, Jun 27, 2012
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. AD
    Replies:
    1
    Views:
    1,046
    Francis Shanahan
    Aug 23, 2003
  2. Flip
    Replies:
    0
    Views:
    300
  3. JGH
    Replies:
    2
    Views:
    682
  4. Andy Chambers
    Replies:
    1
    Views:
    383
    Daniel Dyer
    May 14, 2007
  5. __PaTeR
    Replies:
    7
    Views:
    478
    Barry Schwarz
    Jan 1, 2009
Loading...

Share This Page