我有一个如下所示的 XML 文件:
<A>
<B>
<C a="xyz" b="123" c="x1" flag="yes"> (it in proper formatted)
</C>
<C a="abc" (it is not proper formatted)
b="234" c="y1"
flag="no">
</B>
</A>
Python 中是否有任何方法可以使用任何库将未格式化的行转换为正确的格式?
最佳答案
您可以使用BeautifulSoup parser .
>>> from bs4 import BeautifulSoup
>>> s = '''<A>
<B>
<C a="xyz" b="123" c="x1" flag="yes">
</C>
<C a="abc"
b="234" c="y1"
flag="no">
</B>
</A>'''
>>> soup = BeautifulSoup(s, 'xml')
>>> print(soup.prettify())
<?xml version="1.0" encoding="utf-8"?>
<A>
<B>
<C a="xyz" b="123" c="x1" flag="yes">
</C>
<C a="abc" b="234" c="y1" flag="no">
</C>
</B>
</A>
关于python - 如何通过Python连接未格式化的XML文件中的行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29407978/