[Tutor] Matching zipcode in address file

TGW galaxywatcher at gmail.com
Mon Apr 5 07:15:35 CEST 2010

> Sorry - my mistake - try:
> infile = open("filex")
> match_zips = open("zippys")
> result = [line for line in infile if line in match_zips]
> print result
When I apply the readlines to the original file, It is taking a lot  
longer to process and the outfile still remains blank. Any suggestions?

#!/usr/bin/env python
# Find records that match zipcodes in zips.txt

import os
import sys

def main():
     infile = open("/Users/tgw/NM_2010/NM_APR.txt", "r")
     outfile = open("zip_match_apr_2010.txt", "w")
     zips = open("zips.txt", "r")
     match_zips = zips.readlines()
     lines = [ line for line in infile if line[149:154] in match_zips ]

#    print line[149:154]
     print lines

More information about the Tutor mailing list