我正在使用一个 API 来提取有关噪音水平的数据。当我运行代码时,我收到一个由字符串和整数组成的矩阵。我想把它们放在桌子上。
最后几行代码
r = requests.get(url, params=params, headers=headers)
json_data = r.json()
print(json_data['result'])
结果:
[{'airports': 0, 'traffictext': 'Active', 'localtext': 'Active', 'airportstext': 'Calm', 'score': 73, 'traffic': 25, 'scoretext': 'Active', 'local': 1}]
我想将其转换为两列表格。我也在 TouchDesigner 工作。有人可以帮忙吗?
我尝试读取 JSON 文件,但无法读取它,因为它没有双引号。我试图调出特定的索引来提取与字符串相关的值
'airports':0
示例:
noise_r = json_data['result']
t = noise_r.index('airports')
我对 python 一点也不熟悉,所以我不知道该真正尝试什么。
您可以循环字典来创建表。由于您的 json 数据是一个列表,因此您应该循环该列表以查找其中的所有字典。
典型的实现如下:
data_to_make_table = json_data['result']; # Get the list to iterate over
for dict_in_data in data_to_make_table:
for key, value in dict_in_data.items():
print(key, value)
或者将其列成一个列表:
dict_to_make_table = {"foo": 1, "bar": 2}
result_table = list(dict_to_make_table.items())
print(result_table) # [('foo', 1), ('bar', 2)]