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

Time:02-02

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:

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.

CodePudding user response:

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)
  • Related