python XML文件转JSON文件

import json
import xmltodict


# 定义xml转json的函数
def xmltojson(xmlstr):
    xmlparse = xmltodict.parse(xmlstr)
    jsonstr = json.dumps(xmlparse, indent=1)
    return jsonstr


f = open('province_city_district.xml')
data = f.read()
f2 = open('country.json', 'a')
data2 = xmltojson(data)
data3 = data2.encode('utf-8').decode('unicode_escape')
f2.write(data3)
f2.close()

f3 = open('country.json', 'a')
# print(xmltojson(data))

推荐阅读更多精彩内容