[Tutor] Matching zipcode in address file

bob gailer bgailer at gmail.com
Mon Apr 5 05:47:28 CEST 2010


Please reply-all so a copy goes to the list.

On 4/4/2010 10:02 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?
> I think I am just lost on this one. I have no new insights. What is the exact program that you want me to run?
> #!/usr/bin/env python
>
> infile = open("filex")
> match_zips = open("zippys")
> [line for line in infile if line in match_zips]
> print line
> I did what you said and I get '345' output both times.

Sorry - my mistake - try:

infile = open("filex")
match_zips = open("zippys")
result = [line for line in infile if line in match_zips]
print result



-- 
Bob Gailer
919-636-4239
Chapel Hill NC

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20100404/5349b69f/attachment-0001.html>


More information about the Tutor mailing list