[Tutor] Fastest way to iterate through a file

Robert Hicks sigzero at gmail.com
Tue Jun 26 17:45:22 CEST 2007


Kent Johnson wrote:
> Robert Hicks wrote:
>> idList only has about 129 id numbers in it.
> 
> That is quite a few times to be searching each line of the file. Try 
> using a regular expression search instead, like this:
> 
> import re
> regex = re.compile('|'.join(idList))
> for line in f2:
>    if regex.search(line):
>      # process a hit
> 

Since I am printing to a file like so:

[id]: [line]

I don't see how I can get the id back out of the regex.search like I 
could in my code.

Robert



More information about the Tutor mailing list