python中两个文本文件行的比较

2024-05-15 01:25:20 发布

您现在位置:Python中文网/ 问答频道 /正文

我有两个日志文件,其中包含以下行。我想比较这两个文件中的数据是否相同或不同。

在这个从736.199070736:0x000a00f5)file1.txt数据中,使它成为一行。 会像这样来的

736.199070736:  LOG_MOD_L0_RECEIVE_TXBRP_CONTROL(0, 0x0075007f, 0x005500dd, 0x007f00d7, 0x0057005f, 0x00ff007d, 0x00f700dd, 0x00f50057, 0x000a00f5).

file2.txt中,第一行是:

736.199047132:  LOG_TXBP_MOD_IF_RSP_DPCCH(BlockNum: 0, 0x0075007f, 0x005500dd, 0x007f00d7, 0x0057005f, 0x00ff007d, 0x00f700dd, 0x00f50057, 0x000a00f5)

所以从这两个文件的第一行: 我想比较file1.txt第一行的数据 (0,0x0075007f,0x005500dd,0x007f00d7,0x0057005f,0x00ff007d,0x00f700dd,0x00f50057,0x000a00f5)

以及来自file2.txt第一行的数据 (块编号:0、0x0075007f、0x005500dd、0x007f00d7、0x0057005f、0x00ff007d、0x00f700dd、0x00f50057、0x000a00f5)

我需要删除BlockNum:文本,然后进行比较。

文件1.txt包含:

736.199070736:  LOG_MOD_L0_RECEIVE_TXBRP_CONTROL(0, 
 0x0075007f, 
 0x005500dd, 
 0x007f00d7, 
 0x0057005f, 
 0x00ff007d, 
 0x00f700dd, 
 0x00f50057, 
 0x000a00f5)
 736.209069960: LOG_MOD_L0_RECEIVE_TXBRP_CONTROL(0, 
 0x0075007b, 
 0x005500dd, 
 0x007f00d7, 
 0x0057005f, 
 0x00ff007d, 
 0x00f700dd, 
 0x00f50057, 
 0x000a00f1)

“file2.txt”包含:

736.199047132:  LOG_TXBP_MOD_IF_RSP_DPCCH(BlockNum: 0, 0x0075007f, 0x005500dd, 0x007f00d7, 0x0057005f, 0x00ff007d, 0x00f700dd, 0x00f50057, 0x000a00f5)
736.209044558:  LOG_TXBP_MOD_IF_RSP_DPCCH(BlockNum: 0, 0x0075007f, 0x005500dd, 0x007f00d7, 0x0057005f, 0x00ff007d, 0x00f700dd, 0x00f50057, 0x000a00f5)

我的代码是:

fin1=open("file1.txt","r")
fin2=open("file2.txt","r")
  for line1 in fin1:
      for line2 in fin2:
         if line==line2:
            print "same data"
         else:
            print "data are different"

这和我想要的不太一样。


Tags: 文件数据txtlogmodiffile1control
3条回答

我从您的代码中了解到,将file1的第一行与file2中的所有行进行比较。

实际上,您要做的是读取file1中的一行,如果不同,请将其与file2进行比较reutun“different”并完成。否则,完成所有行的比较并返回相等。

我不喜欢how to compare lines in two files are same or different in python中的答案,因为它们会将所有文件加载到内存中。

我想做的是

f1 = open("file1")
f2 = open("file2")
line1 = next(f1)
line2 = next(f2)
found_different = False
while line1 and line2:
   if line1 != line2:
      found_different = True
      break
   line1 = next(f1)
   line2 = next(f2)

if not line1 and not line2 and not found_different:
   print "equal"
else:
   print "different"

你的问题是在第二个循环中,你会说:

if line==line2:

当需要时:

if line1==line2:

因为它不知道line是什么,所以会出现错误。这是经过测试的正确代码。

fin1=open("file1.txt","r")
fin2=open("file2.txt","r")
for line1 in fin1:
    for line2 in fin2:
        if line1==line2:
            print "same data"
        else:
            print "data are different"
fin1.close()
fin2.close()

快乐编码!

不要只是逐行读取文件,而是过滤每一行:提取()中的内容,如果存在,则删除BlockNum:。像这样的:

def getRecords(fn):
    for line in open(fn, 'r'):
        entry = line.rstrip()[line.find('(')+1:-1]
        if entry.startswith('BlockNum:'):
            yield entry[10:]
        else:
            yield entry

import itertools
filesAreEqual = all(a == b for a, b in itertools.izip(getRecords("file1.txt"),
                                                      getRecords("file2.txt")))

相关问题 更多 >

    热门问题