I have two text files and I want to compare them and save the matched columns to a new text file.

file1:

114.74721

114.85107

114.85107

2.96667

306.61756

file2:

115.06603 0.00294 5.90000

114.74721 0.00674 5.40000

114.85107 0.00453 6.20000

111.17744 0.00421 5.50000

192.77787 0.03080 3.20000

189.70226 0.01120 5.00000

0.46762 0.00883 3.70000

2.21539 0.01290 3.50000

2.96667 0.01000 3.60000

5.43310 0.00393 5.50000

0.28537 0.00497 5.10000

308.82348 0.00183 6.60000

306.61756 0.00359 5.20000

And I want the output to be

114.74721 0.00674 5.40000

114.85107 0.00453 6.20000

114.85107 0.00453 6.20000

2.96667 0.01000 3.60000

306.61756 0.00359 5.20000

I used a script but the output file is empty, Could you help me?
note: some rows in file1.txt have the same value

>>> textfile = file('results.txt', 'wt')
>>> file1 = open("file1.txt", "r")
>>> file2 = open("file2.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:
...   for j in list2:
...     if i==j:
...       file3.write(i)

Recommended Answers

All 5 Replies

Test print list1 and list2 also i and j. You might have to remove all the newline '\n' characters.

From the description you give, what you are talking about is a diff tool. There are many such programs around, but I am assuming this is an assignment, rather than simply the need to diff the two files. The three most common algorithms for the problem (in order of increasing effectiveness) are:

You will probably find it helpful to read them in order, because each builds on the previous work.

I would use this approach ...

with open("file1.txt", "r") as file1:
    file1_str = file1.read()

with open("file2.txt", "r") as file2:
    file2_str = file2.read()

file3_str = ""
for i in file1_str.split('\n'):
    # strip trailing '\n'
    i = i.rstrip()
    for j in file2_str.split('\n'):
        j = j.rstrip()
        # avoid empty values
        if i:
            # value i is in value j
            if i in j:
                print(i, j)  # test
                # build up the result string
                file3_str += j + '\n'

print('-'*50)
print(file3_str)  # test

with open("results.txt", "w") as fout:
    fout.write(file3_str)

''' result ...
114.74721 114.74721 0.00674 5.40000
114.85107 114.85107 0.00453 6.20000
114.85107 114.85107 0.00453 6.20000
2.96667 2.96667 0.01000 3.60000
306.61756 306.61756 0.00359 5.20000
--------------------------------------------------
114.74721 0.00674 5.40000
114.85107 0.00453 6.20000
114.85107 0.00453 6.20000
2.96667 0.01000 3.60000
306.61756 0.00359 5.20000
'''

I used your script but the results.txt file have more rows than the file1.txt which it should have the same number of rows of file1.txt

Not in your original scope. It means you have duplicates in file2.txt. You can use a set to remove those duplicates first.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.