String Fomat Conversion

Stephen Thorne stephen.thorne at gmail.com
Thu Jan 27 04:01:43 EST 2005


On Thu, 27 Jan 2005 00:02:45 -0700, Steven Bethard
<steven.bethard at gmail.com> wrote:
> Stephen Thorne wrote:
> > f = file('input', 'r')
> > labels = f.readline() # consume the first line of the file.
> >
> > Easy Option:
> > for line in f.readlines():
> >   x, y = line.split()
> >   x = float(x)
> >   y = float(y)
> >
> > Or, more concisely:
> > for line in f.readlines():
> >   x, y = map(float, line.split())
> 
> Somewhat more memory efficient:
> 
> lines_iter = iter(file('input'))
> labels = lines_iter.next()
> for line in lines_iter:
>      x, y = [float(f) for f in line.split()]
> 
> By using the iterator instead of readlines, I read only one line from
> the file into memory at once, instead of all of them.  This may or may
> not matter depending on the size of your files, but using iterators is
> generally more scalable, though of course it's not always possible.

I just did a teensy test. All three options used exactly the same
amount of total memory.

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.

Stephen.



More information about the Python-list mailing list