Я пытаюсь сравнить ниже приведены два XML-форматов в питоне, и хотел бы, чтобы ваши материалы на моем подходесравнение XML с помощью питона путаницы
Файл 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>
Файл 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>
Как вы можете присмотреться к тому, что в файле 2 нет строки <p1:feature car8="111" type="tires" unit="percent">511</p1:feature>
в 2-ом абзаце, которая присутствует в файле 1.
Кроме того, в последней строке 2 пункта файла 2 его car="440"
, тогда как в файле 1 это car="448"
Что я хочу:
В файлах я имею дело есть множество таких различий, так вы, ребята, скажите мне, как распечатай такие недостающие строки и неравные числа из этих files.I хотят выхода в следующем виде:
In file two feature car="111", type="tires" and text = 511 is missing
In file two car="448" whereas in file one it is car="440"
Кроме того, вы можете предложить мне идеи и различные методы. Я застрял в этом вопросе с очень долгого времени и хочу немедленно решить эту проблему.
Что я пробовал:
Я использую LXML для работы сравнения, и я попытался с помощью для цикла в следующем порядке:
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"
Благодаря линии к линии подхода сравнения я получаю все неправильные результаты, начиная со второго абзаца обоих файлов, так как одна строка отсутствует во втором файле.
Возможно, стоит попробовать: https://pypi.python.org/pypi/xmldiff – mzjn