我有以下 XML 文件。<root><catalog> <book id="bk101"> <author>Gambardella, Matthew</author> <title>XML Developer's Guide</title> <genre>Computer</genre> <price>44.95</price> <publish_date>2000-10-01</publish_date> <description>An in-depth look at creating applications with XML.</description> </book></catalog><catalog> <book id="bk102"> <author>Gambardella, Matthew</author> <title>XML Developer's Guide</title> <genre>Computer</genre> <price>45.95</price> <publish_date>2000-10-01</publish_date> <description>An in-depth look at creating applications with XML.</description> </book></catalog><catalog> <book id="bk103"> <author>Gambardella, Matthew</author> <title>XML Developer's Guide</title> <genre>Computer</genre> <price>46.95</price> <publish_date>2000-10-01</publish_date> <description>An in-depth look at creating applications with XML.</description> </book></catalog></root>我想通过消除标记来创建另一个 XML。所以,我的新 XML 看起来像 -<catalog> <book id="bk101"> <author>Gambardella, Matthew</author> <title>XML Developer's Guide</title> <genre>Computer</genre> <price>44.95</price> <publish_date>2000-10-01</publish_date> <description>An in-depth look at creating applications with XML.</description> </book></catalog><catalog> <book id="bk102"> <author>Gambardella, Matthew</author> <title>XML Developer's Guide</title> <genre>Computer</genre> <price>45.95</price> <publish_date>2000-10-01</publish_date> <description>An in-depth look at creating applications with XML.</description> </book></catalog>下面是我的代码,我可以通过消除并保留所有必要的行标记来生成字节类。但最终无法将我的字节类转换为 xml 格式并出现以下错误:xml.etree.ElementTree.ParseError:文档元素后的垃圾:第 11 行,第 0 列你能帮忙吗?
2 回答
翻过高山走不出你
TA贡献1875条经验 获得超3个赞
根元素是 XML 的必需元素。
对于文本处理,也许我们可以做
import re
pattern = re.compile("<[/]{0,1}root>")
removed = re.sub(pattern, '', "<root>something</root>");
print(removed)
?
添加回答
举报
0/150
提交
取消