python - 如何提取存储在 JSON 文件中的二维数组?
问题描述
我有以下代码用于打开上述 json 文件并提取数据。但是,我只想要坐标数据,但代码给了我以下错误。
TypeError: string indices must be integers
.
我将如何打印出坐标数据?
{
"type": "Polygon",
"coordinates": [
[
[
-5.84731,
60.5832
],
[
-5.93843,
60.5832
],
[
-2.39097,
60.5832
],
[
-2.39097,
60.5843
],
[
-2.75097,
60.5823
]
]
]
}
import json
f = open('allData.json')
data = json.load(f)
print(data['type']['coordinate'])
f.close()
解决方案
你只需要调用坐标
import json
f = open('allData.json')
data = json.load(f)
print(data['coordinates'])
f.close()
输出
[[[-5.84731, 60.5832], [-5.93843, 60.5832], [-2.39097, 60.5832], [-2.39097, 60.5843], [-2.75097, 60.5823]]]
推荐阅读
- java - 在命令行应用程序中与文本在同一行上获取输入
- ruby-on-rails - Ruby on Rails:来自数据库的背景 CSS 图像
- php - 不为人知??在 symfony 的 public/index.php 中?
- c - 指向始终指向第一个像素的 RGB 结构的指针数组
- vuejs2 - 如何在元素库的表列中使用渲染头函数?
- scala - 无法运行 Scala Play 应用程序抛出 com.typesafe.config.ConfigException$Missing:找不到密钥的配置设置
- php - 从推特网址中删除@?
- asp.net - DotNetNuke MVC 在 div 中包含 cshtml
- ios - 如何在 UIviewcontroller 上使用 UItabbar?
- python - Spyder3 在 Ubuntu 16.04 中崩溃