Seek the one billionth line in a file containing 3 billion lines.

Ant antroy at gmail.com
Wed Aug 8 07:10:32 EDT 2007


On Aug 8, 11:10 am, Bruno Desthuilliers <bruno.
42.desthuilli... at wtf.websiteburo.oops.com> wrote:
> Jay Loden a écrit :
> (snip)
>
> > If we just want to iterate through the file one line at a time, why not just:
>
> > count = 0
> > handle = open('hugelogfile.txt')
> > for line in handle.xreadlines():
> >     count = count + 1
> >     if count == '1000000000':
> >         #do something
>
> for count, line in enumerate(handle):
>      if count == '1000000000':
>          #do something

You'd get better results if the test were:

if count == 1000000000:

Or probably even:

if count == 999999999:

Since the 1 billionth line will have index 999999999.

Cheers,

--
Ant...

http://antroy.blogspot.com/









More information about the Python-list mailing list