将英文标题翻译成中文,不包括任何特殊字符或引号:“使用 xml2json 和希伯来语 d”

2024-10-05 11:42:12 发布

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

我正在尝试将希伯来文内容从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编码和解码,但似乎不起作用。。。 我使用的是python2.7


Tags: inpyjsonreturnliblinexmlencoding
1条回答
网友
1楼 · 发布于 2024-10-05 11:42:12

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

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

相关问题 更多 >

    热门问题