python对省份json的二次编码和二次解码
#-*-coding:utf-8-*- #@Time :2022/4/12 16:07 #@Author:shuaichao #@File :.pywith open('static/mock/area/110100.json', 'r', encoding='GBK')as fp: # json_data = json.load(fp) #@Software: PyCharm import io import json import time from lxml.builder import unicode import utils if __name__ == '__main__': # 第一次解码,去掉字符串和转义字符 with open('static/mock/province.json', 'r', encoding='GBK')as fp: json_data = json.load(fp) f = open('static/mock/province.json', 'w') f.write(json_data) f.close() # 第二次解码,转中文 with open('static/mock/province.json', 'r', encoding='GBK')as fp: json_data = json.load(fp) type = json.dumps(json_data, ensure_ascii=False, indent=4, separators=(',', ': ')) f = open('static/mock/province.json', 'w') f.write(type) f.close() # 获得 省级疫情人数并填入 with open('static/mock/province.json', 'r', encoding='GBK')as fp: json_data = json.load(fp) res = utils.get_province() list_city = [] list_num = [] for r in res: # print(r[0]) # print(r[1]) list_city.append(r[0]) list_num.append(r[1]) list_city_json = [] # try: area = json_data['msg'] for k, v in enumerate(area): # print(v['province']) list_city_json.append(v['province']) for k, v in enumerate(list_city_json): area1 = str(v).strip().replace("市", "").replace("特别行政区","").replace("回族自治区","").replace("维吾尔自治区","").replace("壮族自治区","").replace("省","").replace("自治区","") sign_privince = True for m, n in enumerate(list_city): if area1 == n.strip(): print(n) json_data['msg'][k]['num'] = str(list_num[m]) type = json.dumps(json_data, ensure_ascii=False, indent=4, separators=(',', ': ')) f = open('static/mock/province.json', 'w') f.write(type) f.close() sign_privince = False # else: # if m==len(list_city)-1: # json_data['msg'][k]['num'] = str(0) # type = json.dumps(json_data, ensure_ascii=False, indent=4, separators=(',', ': ')) # f = open('static/mock/province.json', 'w') # f.write(type) # f.close() if sign_privince: json_data['msg'][k]['num'] = str(0) type = json.dumps(json_data, ensure_ascii=False, indent=4, separators=(',', ': ')) f = open('static/mock/province.json', 'w') f.write(type) f.close() with open('static/mock/province.json', 'r', encoding='GBK')as fp: json_data = json.load(fp) type = json.dumps(json_data) type1 = json.dumps(type) f = open('static/mock/province.json', 'w') f.write(type1) f.close()