我有 50 个标签不匹配的 XML 文件,我想使用 python 修复它们。开始标签<names>
与结束标签不同</name>
。谁能指导我。
<breakfast_menu>
<food>
<names>Belgian Waffles</name>
<price>$5.95</price>
<calories>650</calories>
</food>
</breakfast_menu>
BeautifulSoup
做这个:
>>> from bs4 import BeautifulSoup
>>> myxml = # Your posted XML
>>> soup = BeautifulSoup(myxml,'xml')
>>> print soup
<?xml version="1.0" encoding="utf-8"?>
<breakfast_menu>
<food>
<names>Belgian Waffles</names>
<price>$5.95</price>
<calories>650</calories>
</food>
</breakfast_menu>
如果您正在寻找<name></name>
:
>>> for i in soup.findAll('names'):
... i.name = 'name'
...
>>> print soup
<?xml version="1.0" encoding="utf-8"?>
<breakfast_menu>
<food>
<name>Belgian Waffles</name>
<price>$5.95</price>
<calories>650</calories>
</food>
</breakfast_menu>