Reading data from file: x(i),y(i) form

Satish Chimakurthi skchim0 at engr.uky.edu
Sat Feb 28 01:03:51 CET 2004


Hi all,

Thanks very very much for the tremendous response.


Regards,
Satish

----- Original Message ----- 
From: "Mike C. Fletcher" <mcfletch at rogers.com>
To: "Satish Chimakurthi" <skchim0 at engr.uky.edu>
Cc: "python" <python-list at python.org>
Sent: Friday, February 27, 2004 4:10 PM
Subject: Re: Reading data from file: x(i),y(i) form


> 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/
> 
> 
> 
> -- 
> http://mail.python.org/mailman/listinfo/python-list




More information about the Python-list mailing list