m_w__ 2022-01-07 13:07 采纳率: 100%
浏览 151
已结题

为什么使用Python读取JSON文件时,搜索指定关键字Python会报KeyError的错,但JSON文件中确实有对应的关键词?

使用Python读取JSON文件时,搜索指定关键字‘center’Python会报KeyError的错,但JSON文件中确实有‘center’关键词。

这是Python的代码

import json

# 探索数据的结构
filename = 'data/中华人民共和国.json'
with open(filename,encoding='utf-8') as f:
    all_eq_data = json.load(f)

# 以易读的方式存储
# readable_file = 'data/1中华人民共和国.json'
# with open(readable_file, 'w', encoding='utf-8') as f:
#     json.dump(all_eq_data, f, indent=4)# 接受一个JSON数据对象和一个文件对象,并将数据写入这个文件中,indent=4让数据结构匹配缩进量来设置数据的格式

# 提取地区的数量
all_eq_dicts = all_eq_data['features']

# 提取邮政编码和位置信息
adcodes, names, lons, lats = [], [], [], []
for eq_dict in all_eq_dicts:
    adcode = eq_dict['properties']['adcode']
    name = eq_dict['properties']['name']
    lon = eq_dict['properties']['center'][0]
    lat = eq_dict['properties']['center'][1]
    adcodes.append(adcode)
    names.append(name)
    lons.append(lon)
    lats.append(lat)



print(adcodes)
print(names)
print(lons)
print(lats)

这是对应的JSON文件,只截取了一部分

{
    "type": "FeatureCollection",
    "features": [
        {
            "type": "Feature",
            "properties": {
                "adcode": 110000,
                "name": "\u5317\u4eac\u5e02",
                "center": [
                    116.405285,
                    39.904989
                ],
                "centroid": [
                    116.41995,
                    40.18994
                ],
                "childrenNum": 16,
                "level": "province",
                "parent": {
                    "adcode": 100000
                },
                "subFeatureIndex": 0,
                "acroutes": [
                    100000
                ]
            }
      }
}

这是报的错


Traceback (most recent call last):
  File "D:\Python\Py project\py_Learn\JSON\1eq_explore.py", line 21, in <module>
    lon = eq_dict['properties']['center'][0]
KeyError: 'center'

希望有方法可以正常读取到数据

  • 写回答

1条回答 默认 最新

  • CSDN专家-HGJ 2022-01-07 13:46
    关注

    读取成字典后这样取值:

    dic={
        "type": "FeatureCollection",
        "features": [
            {
                "type": "Feature",
                "properties": {
                    "adcode": 110000,
                    "name": "\u5317\u4eac\u5e02",
                    "center": [
                        116.405285,
                        39.904989
                    ],
                    "centroid": [
                        116.41995,
                        40.18994
                    ],
                    "childrenNum": 16,
                    "level": "province",
                    "parent": {
                        "adcode": 100000
                    },
                    "subFeatureIndex": 0,
                    "acroutes": [
                        100000
                    ]
                }
        }],
    }
    for i in range(len(dic['features'])):
        print(dic['features'][i]['properties']['center'][0])
    
    
    

    结果:

    F:\2021\qa\ot3>t4
    116.405285
    
    

    如有帮助,请点采纳。

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论 编辑记录

报告相同问题?

问题事件

  • 系统已结题 1月19日
  • 已采纳回答 1月11日
  • 创建了问题 1月7日

悬赏问题

  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器