943,899 Members | Top Members by Rank

Ad:
  • Python Discussion Thread
  • Marked Solved
  • Views: 1495
  • Python RSS
You are currently viewing page 1 of this multi-page discussion thread
Aug 14th, 2009
0

Find matches in two text files, output them into a third text file

Expand Post »
Hi,
I'm trying to take two text files, compare them, then output the matches into a new text file. I've read the thread started by the1last, but that's outputting differences, not matches. I just started learning about python a couple days ago, so I don't know anything about what syntax or modules I should use.
Any thoughts or suggestions would be appreciated.
Thanks.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
zmjman08 is offline Offline
4 posts
since Aug 2009
Aug 15th, 2009
0

Re: Find matches in two text files, output them into a third text file

I'm new to Python as well, but I think I could offer some help.

Well if you're looking to find lines that match up, instead of words, then you can do something like this:

python Syntax (Toggle Plain Text)
  1. file1 = open("file1.txt", "r")
  2. file2 = open("file2.txt", "r")
  3. file3 = open("file3.txt", "a")
  4.  
  5. file1.seek(0,0)
  6. file2.seek(0,0)
  7.  
  8. list1 = file1.readlines()
  9. list2 = file2.readlines()
  10.  
  11. for i in list1:
  12. for j in list2:
  13. if i == j:
  14. file3.write("FILE 1:",i)
  15. file3.write("FILE 2:",j)

Now if you are talking about words, then you can probably go through each list and use <string>.split() on it, so you have each word in a seperate list item, but I'm not sure if that would work. Just an idea, really.

Hope I helped.
Reputation Points: 10
Solved Threads: 1
Newbie Poster
Zebibyte is offline Offline
8 posts
since Aug 2009
Aug 15th, 2009
0

Re: Find matches in two text files, output them into a third text file

Zebibyte's code idea is correct, although lines 5 & 6 are unnecessary(I think).
Reputation Points: 2
Solved Threads: 30
Posting Whiz in Training
sravan953 is offline Offline
242 posts
since May 2009
Aug 17th, 2009
0

Re: Find matches in two text files, output them into a third text file

Thanks for the replies.
I tried the code that Zebibyte suggested, but I'm getting the following error:

Traceback (most recent call last):
File '/home/zach/bin/ps3.py", line 7, in <module>
list1 = file1.readLines()
AttributeError: 'file' object has no attribute 'readLines'
Reputation Points: 10
Solved Threads: 0
Newbie Poster
zmjman08 is offline Offline
4 posts
since Aug 2009
Aug 17th, 2009
0

Re: Find matches in two text files, output them into a third text file

readLines() is a type error; the 'l' is not capitals. So- readlines()
Last edited by sravan953; Aug 17th, 2009 at 10:46 am. Reason: Code mistake
Reputation Points: 2
Solved Threads: 30
Posting Whiz in Training
sravan953 is offline Offline
242 posts
since May 2009
Aug 17th, 2009
0

Re: Find matches in two text files, output them into a third text file

Oh yeah, thanks.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
zmjman08 is offline Offline
4 posts
since Aug 2009
Aug 17th, 2009
0

Re: Find matches in two text files, output them into a third text file

Thanks to both of you for your help.

Here is the final working version of the code:

Python Syntax (Toggle Plain Text)
  1. #!/usr/bin/env python
  2.  
  3. textfile = file('results.txt', 'wt')
  4.  
  5. file1 = open("list1.txt", "r")
  6. file2 = open("list2.txt", "r")
  7. file3 = open("results.txt", "a")
  8.  
  9. list1 = file1.readlines()
  10. list2 = file2.readlines()
  11.  
  12. file3.write("The following entries appear in both lists: \n")
  13.  
  14. for i in list1:<blockquote>for j in list2:</blockquote><blockquote><blockquote>if i==j:</blockquote></blockquote><blockquote><blockquote><blockquote>file3.write(i)</blockquote></blockquote></blockquote>
Reputation Points: 10
Solved Threads: 0
Newbie Poster
zmjman08 is offline Offline
4 posts
since Aug 2009
Aug 7th, 2010
0
Re: Find matches in two text files, output them into a third text file
What codee would I have to add/replace if i wanted to compare words and not lines?
Reputation Points: 10
Solved Threads: 0
Newbie Poster
eldeingles is offline Offline
6 posts
since Aug 2010
Aug 7th, 2010
0
Re: Find matches in two text files, output them into a third text file
Use difflib.
Featured Poster
Reputation Points: 687
Solved Threads: 748
Industrious Poster
pyTony is offline Offline
4,204 posts
since Apr 2010
Aug 7th, 2010
0
Re: Find matches in two text files, output them into a third text file
I need to compare a text in a txt file against another txt file containing a list of words (one word/expresion per line) to get the words present in the tetx file NOT contained in the list of words written to a new file.

This way i can focus on new words to bullid up a dictionary foro my pupils.
Thx in advance.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
eldeingles is offline Offline
6 posts
since Aug 2010

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Python Forum Timeline: Not able to execute python script from command line
Next Thread in Python Forum Timeline: Interacting with chat servers on Ustream





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC