我有两个文件,其内容如下所示
# version 17.3
# Config info for 0092
# Data for sgs
# State : NA
# Length : NA
# Generated on Wed Apr 29 10:30:12 2015
------------------------------------
rest of the data
------------------------------------
我想在忽略以“#”开头的行后比较这两个文件。
我尝试了文件比较模块,它在 block 级别进行比较(将整个文件作为一个 block 读取并进行比较)。 我写了一个简单的代码来逐行比较,但它似乎不起作用。
def extract_lines(files):
for line in enumerate(files):
if line!= "#":
yield line
with open(file1) as f1:
with open(file2) as f2:
for (line1), (line2) in itertools.izip( extract_lines(f1), extract_lines(f2)):
if line1 != line2:
print "They are different"
break
else:
print "They are identical.
请您参考如下方法:
在 extract_lines 函数中,而不是
for line in enumerate(files):
if line!= "#":
你应该这样做:
for line in files:
if not line.startswith("#"):
