1. 程式人生 > 其它 >python取二次編碼地圖資料進行遍歷修改再二次編碼

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