Can Readlines() go to next line after a Tab

Gabriel Genellina gagsl-py2 at
Wed Jun 27 03:36:23 CEST 2007

En Tue, 26 Jun 2007 22:16:56 -0300, <tim at> escribió:

> I have a module based app that can load many modules at startup. The  
> modules are text based with a '\t' separating the keyword from the  
> definition. The app reads each module to extract the keywords, but  
> because readlines() must read to the end of the current line to get to  
> the next, the module loading is slow.
> Is there a way to have readlines() go to the next line when it finds a  
> tab '\t' instead of a newline '\n'?

If the file is big, the very usage of readlines() slows the process. It  
has to read the entire file into memory.
I'd use something like this instead:

for line in def_file:
     tabpos = line.find("\t")
     if tabpos>0: # >= if an empty keyword is allowed
         keyword = line[:tabpos]

Gabriel Genellina

More information about the Python-list mailing list