diff --git a/Config/Config.py b/Config/Config.py new file mode 100644 index 0000000..5f28244 --- /dev/null +++ b/Config/Config.py @@ -0,0 +1 @@ +EXCEL_PATH=r'D:\dsWork\YunNanProject\Doc\数据库-2015-2024-v2.xlsx' \ No newline at end of file diff --git a/Config/__init__.py b/Config/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/Config/__pycache__/Config.cpython-310.pyc b/Config/__pycache__/Config.cpython-310.pyc new file mode 100644 index 0000000..205bedf Binary files /dev/null and b/Config/__pycache__/Config.cpython-310.pyc differ diff --git a/Config/__pycache__/__init__.cpython-310.pyc b/Config/__pycache__/__init__.cpython-310.pyc new file mode 100644 index 0000000..95f9dd7 Binary files /dev/null and b/Config/__pycache__/__init__.cpython-310.pyc differ diff --git a/Dict/JianCheng.json b/Dict/JianCheng.json new file mode 100644 index 0000000..8ed1fce --- /dev/null +++ b/Dict/JianCheng.json @@ -0,0 +1,43 @@ +[ + {"abbreviation": "石林县", "full_name": "石林彝族自治县", "area_code": "530126000"}, + {"abbreviation": "禄劝县", "full_name": "禄劝彝族苗族自治县", "area_code": "530128000"}, + {"abbreviation": "寻甸县", "full_name": "寻甸回族彝族自治县", "area_code": "530129000"}, + {"abbreviation": "澄江县", "full_name": "澄江市", "area_code": "530481000"}, + {"abbreviation": "峨山县", "full_name": "峨山彝族自治县", "area_code": "530426000"}, + {"abbreviation": "新平县", "full_name": "新平彝族傣族自治县", "area_code": "530427000"}, + {"abbreviation": "元江县", "full_name": "元江哈尼族彝族傣族自治县", "area_code": "530428000"}, + {"abbreviation": "玉龙县", "full_name": "玉龙纳西族自治县", "area_code": "530721000"}, + {"abbreviation": "宁蒗县", "full_name": "宁蒗彝族自治县", "area_code": "530724000"}, + {"abbreviation": "宁洱县", "full_name": "宁洱哈尼族彝族自治县", "area_code": "530821000"}, + {"abbreviation": "墨江县", "full_name": "墨江哈尼族自治县", "area_code": "530822000"}, + {"abbreviation": "景东县", "full_name": "景东彝族自治县", "area_code": "530823000"}, + {"abbreviation": "景谷县", "full_name": "景谷傣族彝族自治县", "area_code": "530824000"}, + {"abbreviation": "镇沅县", "full_name": "镇沅彝族哈尼族拉祜族自治县", "area_code": "530825000"}, + {"abbreviation": "江城县", "full_name": "江城哈尼族彝族自治县", "area_code": "530826000"}, + {"abbreviation": "孟连县", "full_name": "孟连傣族拉祜族佤族自治县", "area_code": "530827000"}, + {"abbreviation": "澜沧县", "full_name": "澜沧拉祜族自治县", "area_code": "530828000"}, + {"abbreviation": "西盟县", "full_name": "西盟佤族自治县", "area_code": "530829000"}, + {"abbreviation": "双江县", "full_name": "双江拉祜族佤族布朗族傣族自治县", "area_code": "530925000"}, + {"abbreviation": "耿马县", "full_name": "耿马傣族佤族自治县", "area_code": "530926000"}, + {"abbreviation": "沧源县", "full_name": "沧源佤族自治县", "area_code": "530927000"}, + {"abbreviation": "楚雄州", "full_name": "楚雄彝族自治州", "area_code": "532300000"}, + {"abbreviation": "禄丰市", "full_name": "禄丰县", "area_code": "532331000"}, + {"abbreviation": "大理州", "full_name": "大理白族自治州", "area_code": "532900000"}, + {"abbreviation": "漾濞县", "full_name": "漾濞彝族自治县", "area_code": "532922000"}, + {"abbreviation": "南涧县", "full_name": "南涧彝族自治县", "area_code": "532926000"}, + {"abbreviation": "巍山县", "full_name": "巍山彝族回族自治县", "area_code": "532927000"}, + {"abbreviation": "红河州", "full_name": "红河哈尼族彝族自治州", "area_code": "532500000"}, + {"abbreviation": "屏边县", "full_name": "屏边苗族自治县", "area_code": "532523000"}, + {"abbreviation": "金平县", "full_name": "金平苗族瑶族傣族自治县", "area_code": "532530000"}, + {"abbreviation": "绿春县", "full_name": "绿春县", "area_code": "532531000"}, + {"abbreviation": "河口县", "full_name": "河口瑶族自治县", "area_code": "532532000"}, + {"abbreviation": "文山州", "full_name": "文山壮族苗族自治州", "area_code": "532600000"}, + {"abbreviation": "西双版纳州", "full_name": "西双版纳傣族自治州", "area_code": "532800000"}, + {"abbreviation": "德宏州", "full_name": "德宏傣族景颇族自治州", "area_code": "533100000"}, + {"abbreviation": "芒市", "full_name": "芒市市", "area_code": "533103000"}, + {"abbreviation": "怒江州", "full_name": "怒江傈僳族自治州", "area_code": "533300000"}, + {"abbreviation": "贡山县", "full_name": "贡山独龙族怒族自治县", "area_code": "533324000"}, + {"abbreviation": "兰坪县", "full_name": "兰坪白族普米族自治县", "area_code": "533325000"}, + {"abbreviation": "迪庆州", "full_name": "迪庆藏族自治州", "area_code": "533400000"}, + {"abbreviation": "维西县", "full_name": "维西傈僳族自治县", "area_code": "533423000"} +] \ No newline at end of file diff --git a/Tools/T2_GetJson.py b/Tools/T2_GetJson.py index eb562ab..606f5ad 100644 --- a/Tools/T2_GetJson.py +++ b/Tools/T2_GetJson.py @@ -2,22 +2,39 @@ # pip install openpyxl import openpyxl -file_name=r'D:\dsWork\YunNanProject\Doc\数据库-2015-2024-v2.xlsx' + +from Config.Config import EXCEL_PATH +from Util.AreaUtil import query_area_info + +file_name = EXCEL_PATH # 读取Excel文件并获取所有sheet名称 try: - # 加载工作簿(添加密码参数) + # 加载工作簿 workbook = openpyxl.load_workbook(file_name, read_only=True) sheet_names = workbook.sheetnames - - print("Excel中的sheet名称列表:") - for idx, sheet in enumerate(sheet_names, 1): - print(f"{idx}. {sheet}") - - # 关闭工作簿释放资源 + + if sheet_names: + first_sheet = workbook[sheet_names[0]] + # 读取所有行数据 + rows = list(first_sheet.iter_rows(values_only=True)) + + # 跳过前两行表头,从第三行开始处理数据 + for row_num, row in enumerate(rows[2:], start=3): + # 假设行政区划名称在第一列(索引0) + if row and row[0]: + area_name = row[0] + result = query_area_info(area_name) + + if result: + print( + f"行 {row_num}: {area_name} -> 全称: {result['full_name']}, 行政区划码: {result['area_code']}") + else: + print(f"行 {row_num}: 未找到 '{area_name}' 的相关信息") + workbook.close() except FileNotFoundError: print(f"错误:找不到文件 '{file_name}'") except Exception as e: - print(f"读取Excel时发生错误:{str(e)}") \ No newline at end of file + print(f"读取Excel时发生错误:{str(e)}") diff --git a/Util/AreaUtil.py b/Util/AreaUtil.py new file mode 100644 index 0000000..7b20836 --- /dev/null +++ b/Util/AreaUtil.py @@ -0,0 +1,102 @@ +import json +import os + + +def load_jiancheng_data(): + """加载简称映射数据""" + # 获取JianCheng.json文件路径 + jiancheng_path = os.path.join( + os.path.dirname(os.path.dirname(__file__)), + 'Dict', + 'JianCheng.json' + ) + + try: + with open(jiancheng_path, 'r', encoding='utf-8') as f: + data = json.load(f) + except FileNotFoundError: + print(f"错误:找不到文件 {jiancheng_path}") + return {}, {} + except json.JSONDecodeError: + print(f"错误:{jiancheng_path} 文件格式不正确") + return {}, {} + + # 创建简称和全称映射字典 + abbrev_map = {} + fullname_map = {} + for item in data: + abbrev = item.get('abbreviation') + fullname = item.get('full_name') + code = item.get('area_code') + if abbrev and fullname and code: + abbrev_map[abbrev] = {'full_name': fullname, 'area_code': code} + fullname_map[fullname] = {'full_name': fullname, 'area_code': code} + + return abbrev_map, fullname_map + + +def load_yunnan_area_data(): + """加载云南省行政区划完整数据""" + area_path = os.path.join( + os.path.dirname(os.path.dirname(__file__)), + 'Dict', + '云南省行政区划数据.json' + ) + + try: + with open(area_path, 'r', encoding='utf-8') as f: + data = json.load(f) + except FileNotFoundError: + print(f"错误:找不到文件 {area_path}") + return {} + except json.JSONDecodeError: + print(f"错误:{area_path} 文件格式不正确") + return {} + + # 递归遍历所有行政区划节点 + area_map = {} + + def traverse_nodes(nodes): + if isinstance(nodes, list): + for node in nodes: + traverse_nodes(node) + elif isinstance(nodes, dict): + # 提取名称和代码 + name = nodes.get('name') + code = nodes.get('code') or nodes.get('area_code') + if name and code: + area_map[name] = {'full_name': name, 'area_code': code} + + # 处理子节点 + children = nodes.get('children') + if children: + traverse_nodes(children) + + traverse_nodes(data) + return area_map + + +def query_area_info(query_str): + """ + 查询行政区划信息 + :param query_str: 简称或全称 + :return: 包含full_name和area_code的字典,未找到返回None + """ + # 加载数据 + abbrev_map, fullname_map = load_jiancheng_data() + yunnan_area_map = load_yunnan_area_data() + + # 1. 先查简称映射表 + if query_str in abbrev_map: + return abbrev_map[query_str] + + # 2. 再查全称映射表 + if query_str in fullname_map: + return fullname_map[query_str] + + # 3. 最后查完整行政区划数据 + if query_str in yunnan_area_map: + return yunnan_area_map[query_str] + + # 未找到 + return None diff --git a/Util/__init__.py b/Util/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/Util/__pycache__/AreaUtil.cpython-310.pyc b/Util/__pycache__/AreaUtil.cpython-310.pyc new file mode 100644 index 0000000..11a32e6 Binary files /dev/null and b/Util/__pycache__/AreaUtil.cpython-310.pyc differ diff --git a/Util/__pycache__/__init__.cpython-310.pyc b/Util/__pycache__/__init__.cpython-310.pyc new file mode 100644 index 0000000..33b9623 Binary files /dev/null and b/Util/__pycache__/__init__.cpython-310.pyc differ