You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何提取JSON文件存储的2D坐标数组并解决TypeError报错问题

报错原因

  • data['type'] 取到的是字符串值 Polygon,你对字符串使用字符串键做二次索引,自然触发 TypeError: string indices must be integers 报错
  • 键名拼写错误,JSON中对应坐标的键是 coordinates,不是你写的 coordinate

正确实现代码

推荐使用 with 上下文管理器自动管理文件关闭,不需要手动调用 close():

import json

with open('allData.json', 'r', encoding='utf-8') as f:
    data = json.load(f)

# 直接打印全部坐标数据
print(data['coordinates'])

# 如果需要遍历取出每一个坐标点,可以参考以下写法
for lon, lat in data['coordinates'][0]:
    print(f"经度:{lon},纬度:{lat}")

内容的提问来源于stack exchange,提问作者Billvid

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.03 19:24:04