import xmltodict
import json
def xml2dict():
file_object = open('1111111018378_20181203214626.xml',encoding = 'utf-8')
try:
all_the_xmlStr = file_object.read()
finally:
file_object.close()
#xml To dict
convertedDict = xmltodict.parse(all_the_xmlStr)
#ensure_ascii 设置为False 中文可以转换
jsonStr = json.dumps(convertedDict,ensure_ascii=False)
print( jsonStr);
#写入文件 写入为utf-8编码
with open('./1111111018378_20181203214626.json', 'w',encoding = 'utf-8') as f:
#除去xmltodict 转换时默认添加的'@' 符号
f.write(jsonStr.replace('@', ''))
#2.Json to Xml
dictVal = {
'page': {
'title': 'xxx',
'ns': 0,
'revision': {
'id': 123456,
}
}
}
convertedXml = xmltodict.unparse(dictVal);
print ("convertedXml=",convertedXml)
if __name__ == '__main__':
xml2dict();
Python3 Xml 转为Dict(字典)
最新推荐文章于 2025-04-02 16:27:50 发布