正如mzjn所提示的,我正在改变整个问题并试图简化它。Python lxml库中没有空标记
我有这样的XML:
<Content Version="1.0" Name="Cont">
<Element Ref="Text_4158" ElementType="ItISSomething" GroupName="Some_Content">
<body>
<p>Some content is here.</p>
</body>
</Element>
<Element Ref="List_585" ElementType="ListElements" GroupName="Lists">
<body>
<p><span class="bold">A list of things</span>: Element1, element2, element3, element4 element5.</p>
</body>
</Element>
</Content>
我要修改的列表的内容和替换 “” 为 “<,>”。我有这样的代码:
from lxml import etree as et
def replace_commas(file):
parser = et.parse(str(file))
root = parser.getroot()
xpath_expr = "//Element[starts-with(@Ref,'List_') \
or @GroupName='Lists']/descendant::*"
elements = root.xpath(xpath_expr)
for element in elements:
if element.text is not None or element.tail is not None:
text = str(element.text)
text = text.replace(',', '<,>')
tail = str(element.tail)
tail = tail.replace(',','<,>')
element.text = text
element.tail = tail
tree = et.ElementTree(root)
tree.write(file, pretty_print=True)
预期输出应该是:
<Content Version="1.0" Name="Cont">
<Element Ref="Text_4158" ElementType="ItISSomething" GroupName="Some_Content">
<body>
<p>Some content is here.</p>
</body>
</Element>
<Element Ref="List_585" ElementType="ListElements" GroupName="Lists">
<body>
<p><span class="bold">A list of things</span>: Element1<,> element2<,> element3<,> element4 element5.</p>
</body>
</Element>
</Content>
但是我的结果是:
<Content Version="1.0" Name="Cont">
<Element Ref="Text_4158" ElementType="ItISSomething" GroupName="Some_Content">
<body>
<p>Some content is here.</p>
</body>
</Element>
<Element Ref="List_585" ElementType="ListElements" GroupName="Lists">
<body>
<p>None<span class="bold">A list of things</span>: Element1<,> element2<,> element3<,> element4 element5.</p>
</body>
</Element>
</Content>
获取标签 “p” 和“跨度之间的无“而且什么都不应该。哪里不对?
我希望此问题的更新有助于了解查询并找到解决方案。
更新:更正了def replace_commas(file)中的冒号:和et.ElementTree(root)的缩进。
另外,我发现mzjn提供的解决方案在哪里给出错误。我在我的xml这个元素:
<Element Ref="List_222"ElementType="ListElements" GroupName="Lists">
<body>
<p><span class="bold">List: <span class="italic">Important elements</span></span>: El1 (prop1), el2 (prop2), el3 (prop3); with a special property.</p>
</body>
</Element>
在此元素我得到了重要元素的尾部NoneType,因为它得到值无。
我看不到如何解决它。
你可以添加理想的情况吗? – Jonathan
我已经添加了理想的情况下,这将与没有None值的空元素,我已经添加了一些关于我的小原因研究的信息。但是我还没有找到任何。 – TMikonos
请简化问题并创建[mcve]。 – mzjn