xml 解析 转json字符串

安装  xmltodict

pip install xmltodict

 

转化代码

import json
import xmltodict


def xml_to_json():
with open('LocList.xml', 'r', encoding='utf8') as f:
xmlStr = f.read()
convertedDict = xmltodict.parse(xmlStr)
jsonStr = json.dumps(convertedDict, indent=1,ensure_ascii=True)
dict_str = json.loads(jsonStr)
with open('LocList.json', 'w', encoding='utf')as f:
f.write(json.dumps(dict_str, ensure_ascii=False))


if __name__ == '__main__':
xml_to_json()

  

posted @ 2020-09-25 17:00  不可描述的细节  阅读(240)  评论(0编辑  收藏  举报