【问题标题】:how to compare lines in two files are same or different in python如何在python中比较两个文件中的行相同或不同
【发布时间】:2013-12-18 05:26:59
【问题描述】:

我有两个文件,其中包含以下几行:

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)

在这里,如果我将文件 1 的输入字符串作为“LOG_MOD_L0_RECEIVE_TXBRP_CONTROL”和“01_LOG_MOD_L0_RECEIVE_TXBRP_CONTROL”作为文件 2。我想检查里面的数据是相同还是不同。我的意思是我必须检查

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

这些数据和

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

此数据是否相同。

我的代码是:

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"

【问题讨论】:

  • 这可能会有所帮助:stackoverflow.com/questions/19007383/…
  • 这里的重点更多是解析文件中的正确部分而不是比较,缺少对文件结构的更好理解
  • @guy 缺少什么结构??
  • 如果我理解正确,您只想比较每个文件的一部分,而文件的结构不是像 csv、xml、json 等明确定义的格式。挑战首先是解析它正确正确地采取相关部分
  • @Guy:只有简单的 .txt 文件

标签: python python-2.7 python-3.x


【解决方案1】:

两个问题:

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

从未使用过,看起来毫无意义,但最重要:

same = set(file1).intersection(file2)

您需要在某处读取文件的内容以比较它们,并且您需要比较两个集合而不是一个集合与一个文件。

还有一个 python 库可以为您执行此操作 - 看看 difflib

【讨论】:

  • same = set(file1).intersection(file2) 没有任何问题...如果 OP 没有先对它们使用 .readlines() :p
【解决方案2】:
#!/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))

打印出来

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

【讨论】:

    【解决方案3】:

    您首先需要解决找到正确的匹配部分的问题。以下生成器函数将生成您要查找的部分信息:

    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
    

    要测试两个文件中是否存在相同的数据,请先读取一个并收集一组中的所有数据:

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

    现在您可以通过设置交集在另一个文件中找到匹配的条目:

    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
    

    【讨论】:

      猜你喜欢
      • 2013-10-01
      • 1970-01-01
      • 2017-06-18
      • 2017-07-10
      • 2014-05-27
      • 2021-11-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多