使用xml2json和希伯来语数据

时间:2016-12-08 14:22:04

标签: python json xml python-2.7 hebrew

我正在尝试将希伯来内容从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

1 个答案:

答案 0 :(得分:0)

使用Unicode字符串,如下所示:

>>> from xml2json import json2xml
>>> json = {"test": u"בדיקה"}
>>> json2xml(json)
'<test>&#1489;&#1491;&#1497;&#1511;&#1492;</test>'