我正在尝试将希伯来内容从json解析为xml,并从xml解析为json。 当我这样做时:
>>> from xml2json import json2xml
>>> json = {"test": "בדיקה"}
>>> json2xml(json)
我收到错误:
Traceback (most recent call last):
File "<pyshell#43>", line 1, in <module>
json2xml(json)
File "C:\Python27\lib\xml2json.py", line 182, in json2xml
return ET.tostring(elem)
File "C:\Python27\lib\xml\etree\ElementTree.py", line 1126, in tostring
ElementTree(element).write(file, encoding, method=method)
File "C:\Python27\lib\xml\etree\ElementTree.py", line 820, in write
serialize(write, self._root, encoding, qnames, namespaces)
File "C:\Python27\lib\xml\etree\ElementTree.py", line 937, in _serialize_xml
write(_escape_cdata(text, encoding))
File "C:\Python27\lib\xml\etree\ElementTree.py", line 1073, in _escape_cdata
return text.encode(encoding, "xmlcharrefreplace")
UnicodeDecodeError: 'ascii' codec can't decode byte 0xe1 in position 0: ordinal not in range(128)
我尝试用utf8进行编码和解码,但它似乎没有工作...... 我正在使用python 2.7
答案 0 :(得分:0)
使用Unicode字符串,如下所示:
>>> from xml2json import json2xml
>>> json = {"test": u"בדיקה"}
>>> json2xml(json)
'<test>בדיקה</test>'