Beginner question : skips every second line in file when usingreadline()

Christopher Koppler klapotec at chello.at
Mon Oct 20 21:34:37 EDT 2003


On Mon, 20 Oct 2003 11:20:22 -0500, "Paul Watson"
<pwatson at redlinec.com> wrote:

>>
>>"Pettersen, Bjorn S" <BjornPettersen at fairisaac.com> wrote in message
>>news:mailman.233.1066625775.2192.python-list at python.org...
>>
>>Assuming that your version of Python is more recent, you can now iterate
>>over file objects using a for loop without having to deal with end of
>>file conditions. A common idiom is:
>>
>>  for line in file(datafile):
>>      ..do stuff..
>>
>
>Does this cause the entire input file to be read into memory before the for
>loop begins execution?
>
>This is great for reading 5 lines, but I might need to read 30 million lines
>from a mortgage company file.  I cannot read the entire file into memory.
>

>From the library reference:

xreadlines()
    This method returns the same thing as iter(f). New in version 2.1.
    Deprecated since release 2.3. Use for line in file instead.

So, this reads the file in line by line, and you can feed your 30
million lines to it without problems...

--
Christopher




More information about the Python-list mailing list