import json import os # 获取脚本所在目录 script_dir = os.path.dirname(os.path.abspath(__file__)) # 构建数据文件的绝对路径 file_path = os.path.join(script_dir, '../Data/RenKou.json') # 读取JSON数据 with open(file_path, 'r', encoding='utf-8') as f: population_data = json.load(f) # 查找云南省的数据 for area in population_data: if area.get('area_name') == '云南省': yunnan_data = area break # 提取2023年的人口数据 year = '2023' # 总人口(万人) total_population = yunnan_data.get('total_population', {}).get(year, 0) # 新生人口(万人,注意原数据是整数,需要转换为万人) birth_population = yunnan_data.get('birth_population', {}).get(year, 0) # 转换为万人(如果是整数) if isinstance(birth_population, int): birth_population = round(birth_population / 10000, 1) # 城镇人口(万人) urban_population = yunnan_data.get('urban_population', {}).get(year, 0) # 乡村人口(万人) rural_population = yunnan_data.get('rural_population', {}).get(year, 0) # 城镇化率(%) urbanization_rate = yunnan_data.get('urbanization_rate', {}).get(year, 0) # 打印结果 print("云南省 2023年人口概览") print("-------------------") print(f"总人口 {total_population:,} 万人") print(f"新生人口 {birth_population} 万人") print(f"城镇人口 {urban_population:,} 万人") print(f"乡村人口 {rural_population:,} 万人") print(f"城镇化率 {urbanization_rate:.2f} %") # 验证数据一致性 if abs(total_population - (urban_population + rural_population)) > 0.01: print("\n注意:总人口与城镇人口+乡村人口存在差异") print(f"差异值: {abs(total_population - (urban_population + rural_population)):.2f} 万人")