| | |
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:
Solved Threads: 0
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.
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.
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:
Now if you are talking about words, then you can probably go through each list and use
Hope I helped.
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)
file1 = open("file1.txt", "r") file2 = open("file2.txt", "r") file3 = open("file3.txt", "a") file1.seek(0,0) file2.seek(0,0) list1 = file1.readlines() list2 = file2.readlines() for i in list1: for j in list2: if i == j: file3.write("FILE 1:",i) 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.
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
•
•
Join Date: Aug 2009
Posts: 4
Reputation:
Solved Threads: 0
Thanks to both of you for your help.
Here is the final working version of the code:
Here is the final working version of the code:
Python Syntax (Toggle Plain Text)
#!/usr/bin/env python textfile = file('results.txt', 'wt') file1 = open("list1.txt", "r") file2 = open("list2.txt", "r") file3 = open("results.txt", "a") list1 = file1.readlines() list2 = file2.readlines() file3.write("The following entries appear in both lists: \n") 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>
![]() |
Similar Threads
- reading multiple text files (Java)
- How often do you read/write to Text Files as a PHP Developer? (PHP)
- Output and read from multiple text files (C++)
- Read line by line from 2 text files and compare both and generate a report!! (C++)
- Help! Concatenating Text Files (C++)
- I need a simple tile based map editor, that will output to a text file. (Game Development)
- How to compile data in several text files into one master file (Python)
- Need reference to Random Accessing of Text files (C++)
- Making arrays from text files (VB.NET)
- Output in Text file-How to apply fprintf()? (C)
Other Threads in the Python Forum
- Previous Thread: Completely reload wxApp
- Next Thread: wxTreeCtrl - events
| Thread Tools | Search this Thread |
Tag cloud for Python
abrupt ansi anti apache approximation array basic beginner book builtin calculator chmod code converter countpasswordentry curved dan08 dictionaries dictionary dynamic examples excel file filename float format ftp function gui heads homework import inches input java launcher library line lines linux list lists loop mouse mysql mysqlquery number numbers numeric output parsing path phonebook plugin port prime program programming progressbar projects py2exe pygame pyqt pysimplewizard python random recursion recursive redirect scrolledtext server software ssh stamp statictext statistics string strings table terminal text textarea thread threading time tkinter tlapse trick tricks tuple tutorial twoup ubuntu unicode urllib urllib2 variable windows wordgame wxpython





