带有 xmltodict unparse() 函数的 ValueError - Python 3
ValueError with xmltodict unparse() function - Python 3
我在使用 xmltodict 将 json 转换为 xml 时遇到问题。它适用于单个根和单个对象,但是当我尝试转换多个对象时它 returns 一个 ValueError "ValueError: document with multiple roots".
这是我的 JSON 数据:
到目前为止,这是我的脚本:
import json
import xmltodict
y = """{{ "markers":[ { "point":"new GLatLng(40.266044,-74.718479)","awayTeam":"LUGip","markerImage":"images/red.png","fixture":"Wednesday 7pm","information":"Linux users group meets second Wednesday of each month.","previousScore":"","capacity":"","homeTeam":"Lawrence Library"},{ "point":"new GLatLng(40.211600,-74.695702)","awayTeam":"LUGip HW SIG","tv":"","markerImage":"images/white.png","fixture":"Tuesday 7pm","information":"Linux users can meet the first Tuesday of the month to work out harward and configuration issues.","capacity":"","homeTeam":"Hamilton Library"},{ "point":"new GLatLng(40.294535,-74.682012)","awayTeam":"After LUPip Mtg Spot","tv":"","markerImage":"images/newcastle.png","fixture":"Wednesday whenever","information":"Some of us go there after the main LUGip meeting, drink brews, and talk.","capacity":"2 to 4 pints","homeTeam":"Applebees"}]}"""
y2 = json.loads(y)
print(xmltodict.unparse(y2, pretty = True))
结果:
Traceback (most recent call last):
File "<ipython-input-89-8838ce8b0d7f>", line 1, in <module>
print(xmltodict.unparse(y2,pretty=True))
File "/Users/luzazul/anaconda/lib/python3.4/site-packages/xmltodict.py", line 323, in unparse
raise ValueError('Document must have exactly one root.')
ValueError: Document must have exactly one root.
非常感谢任何帮助,谢谢!
假设您已清理输入以使其有效(请参阅问题评论)...
看起来 xmltodict 正在尝试为列表中的每个项目创建一个 markers
元素,并且由于 markers
位于顶层,您正在尝试创建多个根。
我会通过在数据周围添加一个顶级元素来解决这个问题,如下所示:
y2 = {'root':y2}
您可以使用xmljson library to convert using different XML JSON conventions。它通过返回一个元素数组来支持多个根。
例如:
>>> import xmljson
>>> xmljson.badgerfish.etree({'x': 1, 'y': 2})
[<Element y at 0x3114a08>, <Element x at 0x3114a48>]
-- x
和 y
元素都被返回。
我在使用 xmltodict 将 json 转换为 xml 时遇到问题。它适用于单个根和单个对象,但是当我尝试转换多个对象时它 returns 一个 ValueError "ValueError: document with multiple roots".
这是我的 JSON 数据:
到目前为止,这是我的脚本:
import json
import xmltodict
y = """{{ "markers":[ { "point":"new GLatLng(40.266044,-74.718479)","awayTeam":"LUGip","markerImage":"images/red.png","fixture":"Wednesday 7pm","information":"Linux users group meets second Wednesday of each month.","previousScore":"","capacity":"","homeTeam":"Lawrence Library"},{ "point":"new GLatLng(40.211600,-74.695702)","awayTeam":"LUGip HW SIG","tv":"","markerImage":"images/white.png","fixture":"Tuesday 7pm","information":"Linux users can meet the first Tuesday of the month to work out harward and configuration issues.","capacity":"","homeTeam":"Hamilton Library"},{ "point":"new GLatLng(40.294535,-74.682012)","awayTeam":"After LUPip Mtg Spot","tv":"","markerImage":"images/newcastle.png","fixture":"Wednesday whenever","information":"Some of us go there after the main LUGip meeting, drink brews, and talk.","capacity":"2 to 4 pints","homeTeam":"Applebees"}]}"""
y2 = json.loads(y)
print(xmltodict.unparse(y2, pretty = True))
结果:
Traceback (most recent call last):
File "<ipython-input-89-8838ce8b0d7f>", line 1, in <module>
print(xmltodict.unparse(y2,pretty=True))
File "/Users/luzazul/anaconda/lib/python3.4/site-packages/xmltodict.py", line 323, in unparse
raise ValueError('Document must have exactly one root.')
ValueError: Document must have exactly one root.
非常感谢任何帮助,谢谢!
假设您已清理输入以使其有效(请参阅问题评论)...
看起来 xmltodict 正在尝试为列表中的每个项目创建一个 markers
元素,并且由于 markers
位于顶层,您正在尝试创建多个根。
我会通过在数据周围添加一个顶级元素来解决这个问题,如下所示:
y2 = {'root':y2}
您可以使用xmljson library to convert using different XML JSON conventions。它通过返回一个元素数组来支持多个根。
例如:
>>> import xmljson
>>> xmljson.badgerfish.etree({'x': 1, 'y': 2})
[<Element y at 0x3114a08>, <Element x at 0x3114a48>]
-- x
和 y
元素都被返回。