简体   繁体   English

如何比较两个文件中的行在python中是相同还是不同

[英]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 在这种情况下,如果我为文件2输入文件1的输入字符串为“ LOG_MOD_L0_RECEIVE_TXBRP_CONTROL”和“ 01_LOG_MOD_L0_RECEIVE_TXBRP_CONTROL”,我想检查其中存在的数据是否相同。我的意思是我必须检查

(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 . 还有一个python库为您执行此操作-查看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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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