比较python中两个文本文件的行 [英] Comparing lines of two text files in python
问题描述
我有两个日志文件,其中包含以下几行.我想比较这两个文件中存在的数据是相同还是不同.
在这个 file1.txt
中,从 736.199070736:
到 0x000a00f5)
的数据放在一行中.会变成这样
736.199070736: LOG_MOD_L0_RECEIVE_TXBRP_CONTROL(0, 0x0075007f, 0x005500dd, 0x007f00d7, 0x0057005f, 0x00ff0007d0x0x7,0f0f0d0x0f007d0
在file2.txt
中的第一行是:
736.199047132: LOG_TXBP_MOD_IF_RSP_DPCCH(BlockNum: 0, 0x0075007f, 0x005500dd, 0x007f00d7, 0x0057005f, 0x07f0d0x50d0d0d0d0d0d0d0f0d0d0x0f0f00
所以从这两个文件的第一行开始:我想比较 file1.txt
第一行的数据(0, 0x0075007f, 0x005500dd, 0x007f00d7, 0x0057005f, 0x00ff007d, 0x00f700dd, 0x00f50057, 0x000a00f5)
和来自 file2.txt
第一行的数据(块编号: 0, 0x0075007f, 0x005500dd, 0x007f00d7, 0x0057005f, 0x00ff007d, 0x00f700dd, 0x00f50057, 0x000a)
我需要删除 BlockNum:
文本然后进行比较.
File1.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, 0x07f0d0x50d0d0d0d0d0d0d0f0d0d0x0f0f00736.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")对于 fin1 中的 line1:对于 fin2 中的 line2:如果行==第2行:打印相同的数据"别的:打印数据不同"
这并没有正确比较我想要的东西.
不要只是逐行读取文件,而是过滤每一行:提取(
)内的内容
code> 并删除 BlockNum:
如果它存在.像这样:
def getRecords(fn):对于 open(fn, 'r') 中的行:entry = line.rstrip()[line.find('(')+1:-1]如果 entry.startswith('BlockNum:'):收益条目[10:]别的:收益进入导入迭代工具filesAreEqual = all(a == b for a, b in itertools.izip(getRecords("file1.txt"),getRecords("file2.txt")))
I have two log files which contains following lines.I want to compare if data present is same or different in both of these files.
In this file1.txt
data from 736.199070736:
to 0x000a00f5)
make it in a single line.
It will come like this
736.199070736: LOG_MOD_L0_RECEIVE_TXBRP_CONTROL(0, 0x0075007f, 0x005500dd, 0x007f00d7, 0x0057005f, 0x00ff007d, 0x00f700dd, 0x00f50057, 0x000a00f5).
And in file2.txt
the first line is:
736.199047132: LOG_TXBP_MOD_IF_RSP_DPCCH(BlockNum: 0, 0x0075007f, 0x005500dd, 0x007f00d7, 0x0057005f, 0x00ff007d, 0x00f700dd, 0x00f50057, 0x000a00f5)
So from the first line of both these files:
I want to compare the data From first line of file1.txt
(0, 0x0075007f, 0x005500dd, 0x007f00d7, 0x0057005f, 0x00ff007d, 0x00f700dd, 0x00f50057, 0x000a00f5)
and the data From first line of file2.txt
(BlockNum: 0, 0x0075007f, 0x005500dd, 0x007f00d7, 0x0057005f, 0x00ff007d, 0x00f700dd, 0x00f50057, 0x000a00f5)
I need to remove the BlockNum:
text and then compare.
File1.txt which contains:
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' contains:
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)
My code is:
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"
This is not comparing properly what I want exactly.
Don't just read the files line by line but filter each line: extract the stuff within (
)
and remove BlockNum:
if it exists. Something like this:
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")))
这篇关于比较python中两个文本文件的行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!