By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,772 Members | 906 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,772 IT Pros & Developers. It's quick & easy.

search between 2 files

P: n/a
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

Jul 18 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
José 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.

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: an*****@despammed.com
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"
Jul 18 '05 #2

P: n/a
On Wed, 04 Feb 2004 13:26:47 +0100, José <jm******@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

Jul 18 '05 #3

P: n/a
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ó:
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


Jul 18 '05 #4

P: n/a
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ó:
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ó:
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


Jul 18 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.