Reading data from file: x(i),y(i) form
Mike C. Fletcher
mcfletch at rogers.com
Fri Feb 27 17:10:46 EST 2004
Satish Chimakurthi wrote:
> Hi all,
>
> I have data of the following form in a file called "fluid_grid.dat"
>
> 1.00000000000000 0.00000000000000D+000
> 0.959753969508636 0.280842158538236
...
> ifile4=open('fluid_grid.dat','r')
> lines=ifile4.readlines( )
> x=[ ]
> y=[ ]
> i=1
> for line in lines:
> x[i],y[i]=map(float,line.replace('D','E').split( ))
> i=i+1
> if i==7:
> break
>
> I get the following error:
>
> x[i],y[i]=map(float,line.replace('D','E').split( ))
> Index Error: list index out of range
You've most likely got a trailing \n\n at the end of the file (or even
in the middle), so you get a NULL line. Simply do something like this:
import traceback
...
for line in ifile4:
line = line.strip().replace( 'D', 'E' )
if line:
values = line.split()
if len(values) == 2:
x.append( values[0] )
y.append( values[1] )
else:
print 'BAD LINE', repr(line)
and you should be able to see what the offending line looks like.
HTH,
Mike
_______________________________________
Mike C. Fletcher
Designer, VR Plumber, Coder
http://members.rogers.com/mcfletch/
More information about the Python-list
mailing list