Python中带有希伯来字符的XML到JSON

时间:2015-05-27 20:51:06

标签: python json xml ascii hebrew

我试图将带有希伯来字符的xml转换为json。

这里是xml的一部分:

     <Root><City>ירושלים</City></Root>

我尝试了几种方法和编码,但我总是得到这个不可转换的ascii代码而不是希伯来字符:

  

\ u05e7 \ u05e0 \ u05d9 \ u05d5 \ u05df

这是我的转换脚本:

import xmltodict, json
import sys

infile = open('text.xml','r')
outfile = open('text.json','w')
xmltxt = infile.read().decode('utf_16_le')

decodedXML = xmltodict.parse(xmltxt)

jsontext = json.dumps(decodedXML, encoding='utf_8')

outfile.write(jsontext.encode('utf_8'))
infile.close()
outfile.close()

谢谢

编辑 - 解决方案: 解决方法是添加&#39; ensure_ascii = False&#39;到json.dumps()

0 个答案:

没有答案
相关问题