Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Compare two files and store unique data in a new file in Python

I have two files where data are stored in a pattern. Here are the details:

file1.txt:

1.1.1.1 -> 0.0.0.0
2.2.2.2 -> 0.0.0.0
3.3.3.3 -> 1.1.1.1
4.4.4.4 -> 2.2.2.2

pattern of file1.txt is like this:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

source ip -> destination ip

file2.txt:

5.5.5.5
6.6.6.6
2.2.2.2
1.1.1.1

pattern of file2.txt is something like this:

source ip

test.py:

with open("file1.txt") as fp1, open("file2.txt") as fp2, open("newfile.txt", "w") as fp3:
    i = 0
    k = 0
    while True:
        try:
            if i == 0:
                # at first get line from both file
                l1 = next(fp1)
                l2 = next(fp2)
            # if both the line is equal get another line
            if l1 == l2:
                try:
                    l1 = next(fp1)
                except StopIteration:
                    break
                l2 = next(fp2)
            # if line are not equal then put l1 in new file
            else:
                fp3.write(l1)
                try:
                    l1 = next(fp1)
                except StopIteration:
                    break
            i += 1
        except StopIteration:
            k += 1
            if k == 2:
                break
        except Exception as e:
            print(e)
            break

My code doesn’t comparing these both files. he is just storing file1.txt data in a new file which is file3.txt. Compare just source ip’s of both files and store data in a new file. The stored data should be like this. File1.txt always compare with file2.txt. if file1.txt data doesn’t exist in file2.txt, that data should be store in a new file which is file3.txt. I have shown my output below:

file3.txt:

3.3.3.3 -> 1.1.1.1
4.4.4.4 -> 2.2.2.2

The common data shouldn’t be store in a new file. Compare the two files and if both of them common data, that common data will be ignored and unique data will be stored in a new file.

>Solution :

You just need some regex knowledge.

import re

input1 = open("file1.txt","r")
input2 = open("file2.txt","r")

output = open("output.txt","w")


file2_data = input2.read()


for line in input1:
    regex_search = re.search(r"\d.\d.\d.\d", line)
    source_ip = regex_search[0]
    if re.search(source_ip,file2_data)==None:
        output.write(line)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading