search between 2 files

  • Thread starter =?ISO-8859-1?Q?Jos=E9?=
  • Start date
?

=?ISO-8859-1?Q?Jos=E9?=

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.

Have anybody any question?

thanks
 
D

Dang Griffith

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
 
A

Andres Rosado-Sepulveda

José said:
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.

Have anybody any question?

I think it would be something like this (iow, untested):

domainsFile = file(filename, "rw")
domains = f1.readlines()
newDomainsFile = file(filename2, "r")
for domain in newDomainsFile:
....if not domain in domains:
........domains.append(domain)

--
Andres Rosado
Email: (e-mail address removed)
ICQ: 66750646
AIM: pantear
Homepage: http://andres980.tripod.com/

"Oh boo! Boo hoo! Oh! ... don't stop now ... you motorheads're killin'
me."
-- A surprisingly alive Rattrap, "A Better Mousetrap"
 
?

=?ISO-8859-1?Q?Jos=E9?=

mmmh, it's not the same, but I try with this indications.

I have a file called "data" from djbdns with different domain, for example:

----------------------
..domain.com:ns.domain.com
+domain.com
+.*domain.com
..domain1.com:ns.domain1.com
+domain.com
+.*domain.com
------------------------

and have a new file for example "list" with new domains

-------------------------
newdomain.com
newdomain1.com
newdomain1.com
-------------------------

I'm programming a little interface in python, and before I modify the
"data" file I'd like to know if I have in the list one domain I have in
data.

Thans




José escribió:
 
?

=?ISO-8859-1?Q?Jos=E9?=

I think with the both solutions it works if I have the files in the same
format, so I think I'll call a script command to have the files in the
same way, and when I learn more python I'll try with regular expressions

:))

thank you very much


José escribió:
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,176
Messages
2,570,950
Members
47,503
Latest member
supremedee

Latest Threads

Top