Home > Software design >  Compare two text files and create another file which contain data along with matching prints
Compare two text files and create another file which contain data along with matching prints

Time:11-04

I have two text files eg1.txt and eg2.txt. I need to compare eg1.txt with respect to eg2.txt and create another file named out.txt .

eg1.txt:

000b423573 bdbaskbjejbajbkjfsjba
00036713dc sjgdjgdgdjadgygdeg263
00123fd351 heqgrg63u1quidg87gduq
0105517f52 vgfeeyguuiduiueyruuur

eg2.txt

0105517f52 vgfeeyguuiduiueyruuur
000b423573 bdbaskbjejbajbkjfsjba
7736001772 absjueui3ryhfuhuffh3u
00123fd351 heqgrg63u1quidg87gduq

output file out.txt

0105517f52 vgfeeyguuiduiueyruuur "Matching with eg1.txt"
000b423573 bdbaskbjejbajbkjfsjba "Matching with eg1.txt"
7736001772 absjueui3ryhfuhuffh3u "Not Matching with eg1.txt"
00123fd351 heqgrg63u1quidg87gduq "Matching with eg1.txt"

This is my current attempt:

file_1 = None
file_2 = None
with open("eg2.txt") as fin_1:
    file_1 = [line.strip() for line in fin_1.readlines()]
with open("eg1.txt") as fin_2:
    file_2 = [line.strip() for line in fin_2.readlines()]

file_3 = [] 
for line in file_2:
    if line not in file_1:
        file_3.append(line)

file_3 = '\n'.join(file_3)
with open("out.txt", "w") as fout:
    fout.write(file_3)
print(f"Wrote file:\n{file_3}")

CodePudding user response:

I notice that in your example you name eg2.txt file_1 and vice versa which leads you to find the lines ineg1.txt which do not exist in eg2.txt. Additionally, you have not yet added logic to concatenate the endings to the line, with can simply be done with . With those slight modifications your code works.

Here is a runnable example!

#!/usr/bin/env python

with open("eg1.txt", "w") as f:
    f.write("""000b423573 bdbaskbjejbajbkjfsjba
00036713dc sjgdjgdgdjadgygdeg263
00123fd351 heqgrg63u1quidg87gduq
0105517f52 vgfeeyguuiduiueyruuur""")

with open("eg2.txt", "w") as f:
    f.write("""0105517f52 vgfeeyguuiduiueyruuur
000b423573 bdbaskbjejbajbkjfsjba
7736001772 absjueui3ryhfuhuffh3u
00123fd351 heqgrg63u1quidg87gduq""")


file_1 = None
file_2 = None
with open("eg1.txt") as fin_1:
    file_1 = [line.strip() for line in fin_1.readlines()]
with open("eg2.txt") as fin_2:
    file_2 = [line.strip() for line in fin_2.readlines()]

file_3 = []
for line in file_2:
    if line in file_1:
        file_3.append(line   '  "Matching with eg1.txt"')
    else:
        file_3.append(line   '  "Not Matching with eg1.txt"')

file_3 = '\n'.join(file_3)
with open("out.txt", "w") as fout:
    fout.write(file_3)
print(f"Wrote file:\n{file_3}")
<script src="https://modularizer.github.io/pyprez/pyprez.min.js" theme="darcula"></script>

CodePudding user response:

You can use list comprehension to write lines along with strings when done comparing to out.txt

with open('eg1.txt') as fin_1, open('eg2.txt') as fin_2, open('out.txt', 'w') as fout:
    file_1, file_2 = fin_1.read().splitlines(), fin_2.read().splitlines()
    fout.write('\n'.join([f'{line} Matching with eg1.txt' if line in file_1
               else f'{line} Not Matching with eg1.txt' for line in file_2]))

# 0105517f52 vgfeeyguuiduiueyruuur Matching with eg1.txt
# 000b423573 bdbaskbjejbajbkjfsjba Matching with eg1.txt
# 7736001772 absjueui3ryhfuhuffh3u Not Matching with eg1.txt
# 00123fd351 heqgrg63u1quidg87gduq Matching with eg1.txt
  • Related