"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
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 =
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
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
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
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
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
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
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
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
/>/
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
"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
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_
13 matches
Mail list logo