python取二次编码地图数据进行遍历修改再二次编码
import json import os import utils if __name__ == '__main__': g = os.walk(r".\static\mock\city") # 取文件,去转义字符 res = utils.get_city() list_city = [] list_num = [] for r in res: list_city.append(r[0]) list_num.append(r[1]) res1 = utils.get_province() list_province = [] list_num1 = [] for r in res1: list_province.append(r[0]) list_num1.append(r[1]) for path, dir_list, file_list in g: for file_name in file_list: tmp=os.path.join(path, file_name) with open(tmp, 'r', encoding='GBK')as fp: json_data = json.load(fp) f = open(tmp, 'w') f.write(json_data) f.close() with open(tmp, 'r', encoding='GBK')as fp: json_data = json.load(fp) type = json.dumps(json_data, ensure_ascii=False, indent=4, separators=(',', ': ')) f = open(tmp, 'w') f.write(type) f.close() with open(tmp, 'r', encoding='GBK')as fp: json_data = json.load(fp) # try: area = json_data['msg'] list_city_json = [] for k, v in enumerate(area): list_city_json.append(v['city']) for k, v in enumerate(list_city_json): sign_city = True sign_province = True area1 = str(v).strip().replace("林区","").replace("土家族苗族自治州","州").replace("地区","").replace("朝鲜族自治州","").replace("盟","").replace("市", "").replace("区", "") for m, n in enumerate(list_city): if area1 == n.strip(): print(area1) print(n) json_data['msg'][k]['num'] = int(list_num[m]) type = json.dumps(json_data, ensure_ascii=False, indent=4, separators=(',', ': ')) f = open(tmp, 'w') f.write(type) f.close() sign_city = False sign_province = False # else: # for s, t in enumerate(list_province): # if area1 == t.strip(): # json_data['msg'][k]['num'] = str(list_num[s]) # type = json.dumps(json_data, ensure_ascii=False, indent=4, # separators=(',', ': ')) # f = open(tmp, 'w') # f.write(type) # f.close() # else: # if s==len(list_province)-1: # json_data['msg'][k]['num'] = str(0) # type = json.dumps(json_data, ensure_ascii=False, indent=4, # separators=(',', ': ')) # f = open(tmp, 'w') # f.write(type) # f.close() if sign_city: for s, t in enumerate(list_province): if area1 == t.strip(): json_data['msg'][k]['num'] = str(list_num[s]) type = json.dumps(json_data, ensure_ascii=False, indent=4, separators=(',', ': ')) f = open(tmp, 'w') f.write(type) f.close() sign_province = False if sign_province: json_data['msg'][k]['num'] = str(0) type = json.dumps(json_data, ensure_ascii=False, indent=4, separators=(',', ': ')) f = open(tmp, 'w') f.write(type) f.close() with open(tmp, 'r', encoding='GBK')as fp: json_data = json.load(fp) type = json.dumps(json_data) type1 = json.dumps(type) f = open(tmp, 'w') f.write(type1) f.close()
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· AI技术革命,工作效率10个最佳AI工具
2021-04-22 python 中获取日期的公式