String Fomat Conversion
Steven Bethard
steven.bethard at gmail.com
Thu Jan 27 12:58:23 EST 2005
Stephen Thorne wrote:
> I did all I did in the name of clarity, considering the OP was on his
> first day with python. How I would actually write it would be:
>
> inputfile = file('input','r')
> inputfile.readline()
> data = [map(float, line.split()) for line in inputfile]
>
> Notice how you don't have to call iter() on it, you can treat it as an
> iterable to begin with.
Beware of mixing iterator methods and readline:
http://docs.python.org/lib/bltin-file-objects.html
next( )
...In order to make a for loop the most efficient way of looping
over the lines of a file (a very common operation), the next() method
uses a hidden read-ahead buffer. As a consequence of using a read-ahead
buffer, combining next() with other file methods (like readline()) does
not work right.
I haven't tested your code in particular, but this warning was enough to
make me generally avoid mixing iter methods and other methods.
Steve
More information about the Python-list
mailing list