如何提取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
相关产品推荐
相关产品推荐

