ExpatError:文档元素xml python后面有垃圾

2024-04-20 15:32:04 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个项目需要在python中将xml转换为dict。我正在使用xmltodict库,但是当我将xml转换为dict时,会出现错误:

Traceback (most recent call last):
  File "<console>", line 1, in <module>
  File "/Users/deanchristianarmada/Desktop/projects/asian_gaming/radar/lib/python2.7/site-packages/xmltodict.py", line 311, in parse
    parser.Parse(xml_input, True)
ExpatError: junk after document element: line 2, column 0

我的代码是:

^{pr2}$

我似乎找不到解决它的方法,而且我没有用在xml中,而是用JSON


Tags: 项目inmost错误linexmlcall中将
1条回答
网友
1楼 · 发布于 2024-04-20 15:32:04

如果在xml字符串的开头添加一个起始根标记,在结尾添加一个结束根标记,那么它应该可以工作。在

    import xmltodict

    xml = 'xml string here'
    xml = '<root>'+xml+'</root>'
    _dict = xmltodict.parse(xml, attr_prefix="")

基本上,它只是缺少了<root>标记。在

相关问题 更多 >