[英]ignoring empty lines when comparing two files
I have this comparison function that takes in two files and compares their content line by line. 我有一个比较功能,可以接收两个文件并逐行比较它们的内容。 Problem is, it doesn't ignore the blank lines. 问题是,它不会忽略空白行。 So if I have the following as File1 所以如果我有以下文件1
Active Internet connections
Proto Recv-Q Send-Q Local Address Foreign Address (state) rxbytes txbytes
tcp4 0 0 192.168.1.6.50860 72.21.91.29.http CLOSE_WAIT 892 691
tcp4 0 0 192.168.1.6.50858 www.v.dropbox.co.https ESTABLISHED 27671 7563
tcp4 0 0 192.168.1.6.50857 162.125.17.1.https ESTABLISHED 17581 3642
and compare this with the following as File2 并将其与以下文件2进行比较
Active Internet connections
Proto Recv-Q Send-Q Local Address Foreign Address (state) rxbytes txbytes
tcp4 0 0 192.168.1.6.50860 72.21.91.29.http CLOSE_WAIT 892 691
tcp4 0 0 192.168.1.6.50858 www.v.dropbox.co.https ESTABLISHED 27671 7563
tcp4 0 0 192.168.1.6.50857 162.125.17.1.https ESTABLISHED 17581 3642
It will take the blank lines into account and compare those empty line with the content in file1. 它将考虑空行,并将这些空行与file1中的内容进行比较。 I've tried using strip()
, but it deletes all the spaces in between each char, which I dont want. 我试过使用strip()
,但是它删除了每个字符之间的所有空格,这是我不想要的。 Is there a way to just delete the empty lines after each line? 有没有办法只删除每行之后的空行? I've highlighted the relevant part of my code. 我已经突出显示了代码的相关部分。
def compare(baseline, newestFile):
baselineHolder = open(baseline)
newestFileHolder = open(newestFile)
lines1 = baselineHolder.readlines()
a = returnName(baseline)
b = returnName(newestFile)
for i,lines2 in enumerate(newestFileHolder):
if not (isEmpty(baseline)) and not (isEmpty(newestFile)):
if lines2 != lines1[i]:
add1 = i + 1
print ("line ", add1, " in newestFile is different \n")
do something like: 做类似的事情:
def non_empty_readline(f):
line = ""
while not line:
line = f.readline().strip()
return line
then call both of those from your code (you need a way to advance to the next line on an empty line without affecting your overall loop structure). 然后从代码中调用这两个代码(您需要一种在不影响整体循环结构的情况下前进至空白行的下一行的方法)。
replace this line 替换这一行
for i,lines2 in enumerate(newestFileHolder):
with 同
for i,lines2 in enumerate(filter(lambda x: len(x) >0 , map(lambda x: x.strip(),newestFileHolder))):
map edits the whitespace and filter removes the lines that are then empty 贴图编辑空白,过滤器删除随后为空的行
Agree with @PeterWood 's comment that difflib is a better way to go 同意@PeterWood的评论,认为difflib是更好的选择
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.