【发布时间】:2021-12-28 04:47:31
【问题描述】:
我有这个 Json(代码中间的 ... 是为了减少这个问题中 Json 的数量,完整的 Json 在这里https://pastebin.com/acKCSq8D):
[
{
"columnHeader": {
"dimensions": [
"ga:city"
],
"metricHeader": {
"metricHeaderEntries": [
{
"name": "ga:users",
"type": "INTEGER"
}
]
}
},
"data": {
"rows": [
{
"dimensions": [
"(not set)"
],
"metrics": [
{
"values": [
"984"
]
}
]
},
{
"dimensions": [
"Sao Paulo"
],
"metrics": [
{
"values": [
"555"
]
}
]
},
...
{
"dimensions": [
"Xanxere"
],
"metrics": [
{
"values": [
"1"
]
}
]
}
],
...
}
}
]
我正在尝试使用“维度”和“值”项,但使用以下代码:
import json
import pandas as pd
with open('test.json') as f:
raw_json = json.load(f)
for i in raw_json:
print(i['data']['rows']['dimensions'])
我收到此错误:
Traceback (most recent call last):
File "C:\Users\gusta\Desktop\Projects\papai\test.py", line 10, in <module>
print(i['data']['rows']['dimensions'])
TypeError: list indices must be integers or slices, not str
如何在不返回此类错误的情况下提取维度等值?
【问题讨论】: