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

编程入门 行业动态 更新时间:2024-10-28 15:31:20
本文介绍了比较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中两个文本文件的行

本文发布于:2023-05-29 16:44:04,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/348720.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:文本文件   两个   python

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!