search between 2 files

Dang Griffith noemail at noemail4u.com
Wed Feb 4 14:51:43 CET 2004


On Wed, 04 Feb 2004 13:26:47 +0100, José <jmcalvar at telecable.es>
wrote:

>I'm newby in Python, and I'm working on a script to manage files.
>I have two files, one with a list of domains, and the other with a new 
>list of domains. I'd like to know if I have a repeated domain in the 
>new's one. I tried opening with readline(), with readlines(), but truly 
>I have no idea to countinue.

You said you wanted to know if you have a repeated domain in the new
one.  I am assuming you mean you want to know if there are any domains
in the new list that are not in the old.  If you just want to
eliminate duplicates from the new list, independently of the first
list, please clarify.

import sets

old_domains = sets.Set()
for domain in file("olddomains.txt"):
    old_domains.add(domain.strip())

new_domains = sets.Set()
for domain in file("newdomains.txt"):
    new_domains.add(domain.strip())

if new_domains == old_domains:
    print "There are no new domains."
else:
    print "The following domains are new:"
    print new_domains - old_domains

--dang
p.s.
Or if you like list comprehensions:

import sets

old_domains = sets.Set()
[old_domains.add(domain.strip()) for domain in file("olddomains.txt")]

new_domains = sets.Set()
[new_domains.add(domain.strip()) for domain in file("newdomains.txt")]

if new_domains == old_domains:
    print "There are no new domains."
else:
    print "The following domains are new:"
    print new_domains - old_domains




More information about the Python-list mailing list