extract substring by regex from a text file

Stefan Behnel stefan_ml at behnel.de
Sat Apr 17 10:58:47 CEST 2010


Alessio, 17.04.2010 10:19:
> I used readlines() to read my text file, then with a for cicle I
> extract line by line the substrings I need by regular expressions
> (re.findall())

Note that it's usually more efficient to just run the for-loop over the 
file object, rather than using readlines() first. The latter will read all 
lines into a big list in memory before doing any further processing, 
whereas the plain for-loop will read line by line and let the loop body act 
on each line immediately.

Stefan




More information about the Python-list mailing list