简体   繁体   中英

how to compare lines in two files are same or different in python

I have two files which contains following lines:

file1:
6.959999999:    LOG_MOD_L0_RECEIVE_TXBRP_CONTROL(0, 
 0x0059005f, 
 0x0049006d, 
 0x00b9008b, 
 0x001300b9)
7.959999999:    LOG_MOD_L0_RECEIVE_TXBRP_Measure(1, 
 0x0059005m, 
 0x0049006d, 
 0x04b9008b, 
 0x001300b9)

file2:
6.959999999:    01_LOG_MOD_L0_RECEIVE_TXBRP_CONTROL(0, 
 0x0059005f, 
 0x0049006d, 
 0x00b9008b, 
 0x001300b9)
7.959999999:    LOG_MOD_L0_RECEIVE_TXBRP_Measure(1, 
 0x0059005m, 
 0x0049006d, 
 0x04b9008b, 
 0x001300b9)

In this if i give input string for file1 as "LOG_MOD_L0_RECEIVE_TXBRP_CONTROL" and "01_LOG_MOD_L0_RECEIVE_TXBRP_CONTROL" for file 2.I want to check the data present inside is same or different.I mean i have to check

(0, 
 0x0059005f, 
 0x0049006d, 
 0x00b9008b, 
 0x001300b9)

this data and

(0, 
 0x0059005f, 
 0x0049006d, 
 0x00b9008b, 
 0x001300b9)

this data both are same or not.

My code is:

file1=open("C:\\Python27\\output1.txt","r")
file2=open("C:\\Python27\\output2.txt","r")
lines1=file1.readlines()
lines2=file2.readlines()

output1_string=raw_input("Enter the String of file1:")
output2_string=raw_input("Enter the String of file2:")
for line1 in lines1:
    for line2 in lines2:
      if line1==line2:
         print "both are same"
      else:
         print "Different"

Two problems:

output1_string=raw_input("Enter the String of file1:")
output2_string=raw_input("Enter the String of file2:")

Is never used and seems pointless but most important:

same = set(file1).intersection(file2)

You need to read the content of the files somewhere to compare them and you need to compare two sets not a set with a file.

There is also a python library to do this for you -take a look at difflib .

#!/usr/bin/env python3

import re
lines1=open("output1.txt","rt").read()
lines2=open("output2.txt","rt").read()
hits1 = re.findall(r'\(.*?\)', lines1, re.DOTALL)
hits2 = re.findall(r'\(.*?\)', lines2, re.DOTALL)
print('equal:', set(hits1).intersection(hits2))
print('diff: ', set(hits1).difference(hits2))

Prints out

equal: {'(0, \n 0x0059005f, \n 0x0049006d, \n 0x00b9008b, \n 0x001300b9)', '(1, \n 0x0059005m, \n 0x0049006d, \n 0x04b9008b, \n 0x001300b9)'}
diff:  set()

You'll first need to solve the problem of locating the right, matching section. The following generator function will produce the section information you are looking for:

def find_sections(filename, text):
    with open(filename) as fin:
        section = None
        for line in fin:
            if text in line:
                section = line.rpartition('(')[-2:]
                try:
                    while ')' not in line:
                        line = next(fin)
                        section.append(line)
                except StopIteration:
                    pass  # ran out of file to read
                yield ''.join(section)
            else:
                previous = line

To test if the same data exists in both files, read one first and collect all data in a set:

output1_string=raw_input("Enter the String of file1:")
sections1 = set(find_sections("C:\\Python27\\output1.txt", output1_string))

Now you can find matching entries in the other file by doing a set intersection:

output2_string=raw_input("Enter the String of file2:")
sections2 = find_sections("C:\\Python27\\output2.txt", output1_string)
for match in sections1.intersection(sections2):
    print 'Found a match:'
    print match

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM