Re: [Tutor] Matching zipcode in address file

2010-04-06 Thread Alan Gauld
"TGW" wrote I got it. I was comparing '345' to '345\n' Adding the '\n' to the slice did indeed do the trick. Yes, the problem is that the data in the file always has a \n at the end. So you either have to rstrip() that off when you read it from the file or add a \n to your source data when

Re: [Tutor] Matching zipcode in address file

2010-04-05 Thread TGW
I got it. I was comparing '345' to '345\n' Adding the '\n' to the slice did indeed do the trick. #!/usr/bin/env python import string def main(): infile = open("filex") outfile = open("results_testx", "w") zips = open("zippys", "r") match_zips = zips.readlines() lines =

Re: [Tutor] Matching zipcode in address file

2010-04-05 Thread TGW
OK - you handled the problem regarding reading to end-of-file. Yes it takes a lot longer, because now you are actually iterating through match_zips for each line. How large are these files? Consider creating a set from match_zips. As lists get longer, set membership test become faster than list

Re: [Tutor] Matching zipcode in address file

2010-04-05 Thread bob gailer
On 4/5/2010 1:15 AM, TGW wrote: 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

Re: [Tutor] Matching zipcode in address file

2010-04-05 Thread ALAN GAULD
Please use Reply All whern responding to the list. > lines = [line for line in infile if line[149:154] not in match_zips] > >Nope. I tried that. I actually modified your comprehension >that you provided about a month ago. >Works great for NOT matching, but can't figure out how to match. >Do

Re: [Tutor] Matching zipcode in address file

2010-04-04 Thread TGW
I'd suggest reading the data from the match_zips into a list, and if the format isn't correct, doing some post-processing on it. But there's no way to advise on that since we weren't given the format of either file. zipdata = match_zips.readlines() Then you can do an if XXX in zipdata

Re: [Tutor] Matching zipcode in address file

2010-04-04 Thread Dave Angel
Alan Gauld wrote: "TGW" wrote 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? Take out the word 'not' from the comprehension? T

Re: [Tutor] Matching zipcode in address file

2010-04-04 Thread TGW
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

Re: [Tutor] Matching zipcode in address file

2010-04-04 Thread TGW
Sorry - my mistake - try: infile = open("filex") match_zips = open("zippys") result = [line for line in infile if line in match_zips] print result okThanks...This should do it: #!/usr/bin/env python infile = open("filex") zips = open("zippys") match_zips = zips.readlines() results = [lin

Re: [Tutor] Matching zipcode in address file

2010-04-04 Thread bob gailer
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 />/

Re: [Tutor] Matching zipcode in address file

2010-04-04 Thread bob gailer
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

Re: [Tutor] Matching zipcode in address file

2010-04-04 Thread Alan Gauld
"TGW" wrote 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? Take out the word 'not' from the comprehension? -- Alan Gauld Author

[Tutor] Matching zipcode in address file

2010-04-04 Thread TGW
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_