Seek the one billionth line in a file containing 3 billion lines.
Bruno Desthuilliers
bruno.42.desthuilliers at wtf.websiteburo.oops.com
Wed Aug 8 08:02:56 EDT 2007
Ant a écrit :
> 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.
>
Doh :(
Thanks for the correction.
More information about the Python-list
mailing list