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 腾讯云如何建立同一个项目中物模型之间的联系
  • ¥30 VMware 云桌面水印如何添加
  • ¥15 用ns3仿真出5G核心网网元
  • ¥15 matlab答疑 关于海上风电的爬坡事件检测
  • ¥88 python部署量化回测异常问题
  • ¥30 酬劳2w元求合作写文章
  • ¥15 在现有系统基础上增加功能
  • ¥15 远程桌面文档内容复制粘贴,格式会变化
  • ¥15 这种微信登录授权 谁可以做啊
  • ¥15 请问我该如何添加自己的数据去运行蚁群算法代码