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

 

posted @ 2022-04-22 14:47  帅超007  阅读(37)  评论(0编辑  收藏  举报