[Tutor] Omitting lines matching a list of strings from a file

galaxywatcher at gmail.com galaxywatcher at gmail.com
Thu Feb 25 08:02:48 CET 2010

I am trying to output a list of addresses that do not match a list of  
State abbreviations. What I have so far is:

def main():
     infile = open("list.txt", "r")
     for line in infile:
         state = line[146:148]
         omit_states = ['KS', 'KY', 'MA', 'ND', 'NE', 'NJ', 'PR',  
'RI', 'SD', 'VI', 'VT', 'WI']
         for n in omit_states:
             if state != n:
                 print line

This outputs multiple duplicate lines. The strange thing is that if I  
change 'if state == n:' then I correctly output all matching lines.  
But I don't want that. I want to output all lines that do NOT match  
the States in the omit_states list.

I am probably overlooking something very simple. Thanks in advance.

More information about the Tutor mailing list