How to change a generator ?

MRAB google at mrabarnett.plus.com
Wed Dec 24 13:00:26 EST 2008


Gabriel Genellina wrote:
> En Wed, 24 Dec 2008 15:03:58 -0200, MRAB <google at mrabarnett.plus.com> 
> escribió:
> 
>>>  I have a generator whose aim is to returns consecutive lines from a 
>>> file (the listing below is a simplified version).
>>> However, as it is written now, the generator method changes the text 
>>> file pointer to end of file after first invocation.
>>> Namely, the file pointer changes from 0 to 6623 on line 24.
>>>
>> It might be that the generator method of self.input_file is reading 
>> the file a chunk at a time for efficiency even though it's yielding a 
>> line at a time.
> 
> I think this is the case too.
> I can think of 3 alternatives:
> 
> a) open the file unbuffered (bufsize=0). But I think this would greatly 
> decrease performance.
> 
> b) keep track internally of file position (by adding each line length). 
> The file should be opened in binary mode in this case (to avoid any '\n' 
> translation).
> 
> c) return line numbers only, instead of file positions. Seeking to a 
> certain line number requires to re-read the whole file from start; 
> depending on how often this is required, and how big is the file, this 
> might be acceptable.
> 
readline() appears to work as expected, leaving the file position at the 
start of the next line.



More information about the Python-list mailing list