[newbie] problem with data (different behaviour between batch and interactive use)
Cousin Stanley
cousinstanley at gmail.com
Wed Jun 27 09:23:34 EDT 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
More information about the Python-list
mailing list