[Tutor] Matching zipcode in address file
bob gailer
bgailer at gmail.com
Mon Apr 5 02:24:52 CEST 2010
On 4/4/2010 5:18 PM, TGW wrote:
> I wrote a script that compares two text files (one zip code file, and
> one address file) and tries to output records that match the
> zipcodes. Here is what I have so far:
>
> #!/usr/bin/env python
> # Find records that match zipcodes in zips.txt
>
> def main():
> infile = open("/Users/tgw/NM_2010/NM_APR.txt", "r")
> outfile = open("zip_match_apr_2010.txt", "w")
> match_zips = open("zips.txt", "r")
>
> lines = [line for line in infile if line[149:154] in match_zips] #
> *** I think the problem is here ***
Yep. You are right.
Try a very simple test case; see if you can figure out what's happening:
infile:
123
234
345
match_zips:
123
234
345
infile = open("infile")
match_zips = open("match_zips")
[line for line in infile if line in match_zips]
Now change infile:
123
244
345
and run the program again.
Interesting, no. Does that give you any insights?
>
> outfile.write(''.join(lines))
> infile.close()
> outfile.close()
> main()
>
> I go the program functioning with
> lines = [line for line in infile if line[149:154] not in match_zips]
>
> But this matches records that do NOT match zipcodes. How do I get this
> running so that it matches zips?
>
> Thanks
--
Bob Gailer
919-636-4239
Chapel Hill NC
More information about the Tutor
mailing list