Nested loop not working

MRAB python at mrabarnett.plus.com
Fri Jul 16 10:59:28 EDT 2010


Johann Spies wrote:
> I am overlooking something stupid.
> 
> I have two files: one with keywords and another with data (one record per line).
> 
> I want to determine for each keyword which lines in the second file
> contains that keyword.
> 
> The following code is not working.  It loops through the second file
> but only uses the first keyword in the first file.
> 
> #!/usr/bin/env python
> # -*- coding: utf-8 -*-
> 
> import re
> 
> keywords = open("sleutelwoorde",'r')
> data = open("sarua_marine_sleutelwoorde.csv",'r')
> 
> remove_quotes = re.compile('"')
> 
> 
> for sw in keywords:
>     for r in data:
>         swc = remove_quotes('',sw)[:-1]
>         if swc in r.lower():
>                 print swc + ' ---> ' + r
>                 print swc
> 
> What am I missing?
> 
The line:

     for r in data

reads through the file until it the end. The next time around the outer
loop it's already at the end of the file. You need to reset it to the
start of the file with:

     data.seek(0)

Incidentally, it would be faster if you read the keywords into a list
first (assuming that there isn't a huge number of keywords) and then
scanned through the file once.



More information about the Python-list mailing list