enumerate overflow

Gabriel Genellina gagsl-py2 at yahoo.com.ar
Thu Oct 4 19:07:33 EDT 2007


En Wed, 03 Oct 2007 08:46:31 -0300, <crwe at post.cz> escribi�:

> in python2.4, i read lines from a file with
>
> for lineNum, line in enumerate(f): ...
>
> However, lineNum soon overflows and starts counting backwards. How do
> i force enumerate to return long integer?

(what kind of files are you using? enumerate overlows after more than two  
billion lines... is that "soon" for you?)

I'm afraid neither iterate nor itertools.count will generate a long  
integer; upgrading to Python 2.5 won't help. I think the only way is to  
roll your own counter:

lineNum = 0
for line in f:
   ...
   lineNum += 1

-- 
Gabriel Genellina




More information about the Python-list mailing list