安装 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()