urllib2 - iteration over non-sequence

Paul Rubin http
Sun Jun 10 04:02:29 CEST 2007


Erik Max Francis <max at alcyone.com> writes:
> This is really wasteful, as there's no point in reading in the whole
> file before iterating over it.  To get the same effect as file
> iteration in later versions, use the .xreadlines method::
> 
> 	for line in aFile.xreadlines():
> 	    ...

Ehhh, a heck of a lot of web pages don't have any newlines, so you end
up getting the whole file anyway, with that method.  Something like

   for line in iter(lambda: aFile.read(4096), ''): ...

may be best.



More information about the Python-list mailing list