xml comparison using python confusion -
i trying compare below given 2 xml formats in python , have inputs on approach
file 1:
<p1:car> <p1:feature car="111" type="color">511</p1:feature> <p1:feature car="223" type="color">542</p1:feature> <p1:feature car="299" type="color">559</p1:feature> <p1:feature car="323" type="color">564</p1:feature> <p1:feature car="353" type="color">564</p1:feature> <p1:feature car="391" type="color">570</p1:feature> <p1:feature car="448" type="color">570</p1:feature> <p1:feature car="111" type="tires" unit="percent">511</p1:feature> <p1:feature car="223" type="tires" unit="percent">513</p1:feature> <p1:feature car="299" type="tires" unit="percent">516</p1:feature> <p1:feature car="323" type="tires" unit="percent">516</p1:feature> <p1:feature car="353" type="tires" unit="percent">518</p1:feature> <p1:feature car="391" type="tires" unit="percent">520</p1:feature> <p1:feature car="448" type="tires" unit="percent">520</p1:feature> </p1:car> file 2:
<p1:car> <p1:feature car="111" type="color">511</p1:feature> <p1:feature car="223" type="color">542</p1:feature> <p1:feature car="299" type="color">559</p1:feature> <p1:feature car="323" type="color">564</p1:feature> <p1:feature car="353" type="color">564</p1:feature> <p1:feature car="391" type="color">570</p1:feature> <p1:feature car="448" type="color">570</p1:feature> <p1:feature car="223" type="tires" unit="percent">513</p1:feature> <p1:feature car="299" type="tires" unit="percent">516</p1:feature> <p1:feature car="323" type="tires" unit="percent">516</p1:feature> <p1:feature car="353" type="tires" unit="percent">518</p1:feature> <p1:feature car="391" type="tires" unit="percent">520</p1:feature> <p1:feature car="440" type="tires" unit="percent">520</p1:feature> </p1:car> as can closely in file 2 there no line <p1:feature car8="111" type="tires" unit="percent">511</p1:feature> in 2nd paragraph present in file 1.
also in last line of 2nd paragraph of file 2 car="440"whereas in file 1 car="448"
what want:
in files dealing there numerous such differences can guys tell me how printout such missing lines , unequal numbers these files.i want output in following form:
in file 2 feature car="111", type="tires" , text = 511 missing in file 2 car="448" whereas in file 1 car="440" also, can suggest me ideas , different methods. stuck in question long time , want solve immediately.
what tried:
i using lxml comparison work , tried using loop in following manner:
for i,j in zip(file1.getchildren(),file2.getchildren()): if (int(i.get("car")) & int(i.text)) != (int(j.get("car")) & int(j.text)): print "difference of both files" due line line approach of comparison getting wrong results starting 2nd paragraph of both files since 1 line missing 2nd file.
i think want difflib. please take official documentation here.
in general words, want is:
from difflib import differ text_1 = file_1.read() # getting xml contents text_2 = file_2.read() # getting xml contents second file d = differ() result = d.compare(text_1, text_2) for more details usage please refer official documentation.
Comments
Post a Comment