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

Please support our Python advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved

Join Date: Aug 2009
Posts: 4
Reputation: zmjman08 is an unknown quantity at this point 
Solved Threads: 0
zmjman08 zmjman08 is offline Offline
Newbie Poster

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

 
0
  #1
Aug 14th, 2009
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.
Reply With Quote Quick reply to this message  
Join Date: Aug 2009
Posts: 8
Reputation: Zebibyte is an unknown quantity at this point 
Solved Threads: 1
Zebibyte's Avatar
Zebibyte Zebibyte is offline Offline
Newbie Poster

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

 
0
  #2
Aug 15th, 2009
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:

  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.
Reply With Quote Quick reply to this message  
Join Date: May 2009
Posts: 232
Reputation: sravan953 has a little shameless behaviour in the past 
Solved Threads: 28
sravan953's Avatar
sravan953 sravan953 is offline Offline
Posting Whiz in Training

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

 
0
  #3
Aug 15th, 2009
Zebibyte's code idea is correct, although lines 5 & 6 are unnecessary(I think).
Reply With Quote Quick reply to this message  
Join Date: Aug 2009
Posts: 4
Reputation: zmjman08 is an unknown quantity at this point 
Solved Threads: 0
zmjman08 zmjman08 is offline Offline
Newbie Poster

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

 
0
  #4
Aug 17th, 2009
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'
Reply With Quote Quick reply to this message  
Join Date: May 2009
Posts: 232
Reputation: sravan953 has a little shameless behaviour in the past 
Solved Threads: 28
sravan953's Avatar
sravan953 sravan953 is offline Offline
Posting Whiz in Training

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

 
0
  #5
Aug 17th, 2009
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
Reply With Quote Quick reply to this message  
Join Date: Aug 2009
Posts: 4
Reputation: zmjman08 is an unknown quantity at this point 
Solved Threads: 0
zmjman08 zmjman08 is offline Offline
Newbie Poster

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

 
0
  #6
Aug 17th, 2009
Oh yeah, thanks.
Reply With Quote Quick reply to this message  
Join Date: Aug 2009
Posts: 4
Reputation: zmjman08 is an unknown quantity at this point 
Solved Threads: 0
zmjman08 zmjman08 is offline Offline
Newbie Poster

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

 
0
  #7
Aug 17th, 2009
Thanks to both of you for your help.

Here is the final working version of the code:

  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>
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



Tag cloud for Python
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC