Reputation: 63
I've got two files and its content looks like below
# 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
------------------------------------
I want to compare the 2 files after ignoring the lines start with "#".
I tried file compare module, it does comparison at block level(reading the whole file as a block and comparing it).
I wrote a simple code to do line by line comparison and it doesn't seem to work.
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.
Upvotes: 1
Views: 1250
Reputation: 180401
I presume you are comparing the lines in pairs due to zipping, you can zip them together using two generator expressions to filter lines starting with #
:
from itertools import izip
with open("in1.txt") as f, open("in2.txt") as f2:
a = (line for line in f if not line.startswith("#"))
b = (line for line in f2 if not line.startswith("#"))
for l1, l2 in zip(a ,b):
print(l1,l2)
Upvotes: 2
Reputation: 81594
In extract_lines
function, instead of
for line in enumerate(files):
if line!= "#":
you should do:
for line in files:
if not line.startswith("#"):
Upvotes: 3