# 读取 Dict 目录下 ChinaCitys2025.json
import json
with open(r'D:\dsWork\YunNanProject\Dict\ChinaCitys2025.json', 'r', encoding='utf-8') as f:
    data = json.load(f)
    # 筛选云南省数据并格式化输出
    try:
        # 查找名称为"云南省"的条目
        yunnan_data = next(item for item in data if item.get('name') == '云南省')
        
        # 仅保留到县区级，移除街道数据
        for city in yunnan_data.get('citys', []):
            for area in city.get('areas', []):
                # 删除乡镇/街道层级数据
                area.pop('towns', None)
                
        # 格式化输出
        print(json.dumps(yunnan_data, ensure_ascii=False, indent=4))
        
        with open('../Dict/云南省行政区划数据.json', 'w', encoding='utf-8') as outfile:
            json.dump(yunnan_data, outfile, ensure_ascii=False, indent=4)
            print("\n数据已保存至：云南省行政区划数据.json")
            
    except StopIteration:
        print("错误：未找到云南省相关记录")
    except Exception as e:
        print(f"处理数据时发生错误：{str(e)}")