Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
xmltodict unparse()函数的ValueError-Python 3_Python_Xml_Json_Xmltodict - Fatal编程技术网

xmltodict unparse()函数的ValueError-Python 3

xmltodict unparse()函数的ValueError-Python 3,python,xml,json,xmltodict,Python,Xml,Json,Xmltodict,我在使用xmltodict将json转换为xml时遇到问题。它可以很好地处理单个根和单个对象,但是当我尝试转换多个对象时,它会返回一个ValueError“ValueError:documentwithmultipleroot” 以下是我的JSON数据: 以下是我目前的剧本: import json import xmltodict y = """{{ "markers":[ { "point":"new GLatLng(40.266044,-74.718479)","awayT

我在使用xmltodict将json转换为xml时遇到问题。它可以很好地处理单个根和单个对象,但是当我尝试转换多个对象时,它会返回一个ValueError“ValueError:documentwithmultipleroot”

以下是我的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.
回溯(最近一次呼叫最后一次):
文件“”,第1行,在
打印(xmltodict.unpasse(y2,pretty=True))
文件“/Users/luzazul/anaconda/lib/python3.4/site packages/xmltodict.py”,第323行,未解析
raise VALUERROR('文档必须只有一个根')
ValueError:文档必须只有一个根。

任何帮助都将不胜感激,谢谢

假设您已清理输入以使其有效(请参阅对问题的评论)

看起来xmltodict正在尝试为列表中的每个项目创建一个
标记
元素,并且由于
标记
位于顶层,因此您正在尝试创建多个根

我会在数据周围添加一个顶级元素,如下所示:

y2 = {'root':y2}
您可以使用库使用不同的方法进行转换。它通过返回元素数组来支持多个根

例如:

>>> import xmljson
>>> xmljson.badgerfish.etree({'x': 1, 'y': 2})
[<Element y at 0x3114a08>, <Element x at 0x3114a48>]
导入xmljson >>>xmljson.badgerfish.etree({'x':1,'y':2}) [, ]
--返回
x
y
元素。

您的字符串
y
不是有效的json或python——json需要双引号表示dict键,python需要三引号表示多行字符串。我甚至不知道您是如何使用无效python和无效json得到结果的。