【发布时间】:2020-07-21 11:43:49
【问题描述】:
目前我正在尝试从包含日期和国家/地区的 json 文件中获取“严格”数据。以下是 json 输出的摘录:
import pandas as pd
import json
from bs4 import BeautifulSoup
# load file
with open("Stringency April 8.txt") as file:
stringency_data = json.load(file)
stringency_data["data"]
#this gives the output:
{'2020-01-02': {'ABW': {'confirmed': None,`
'country_code': 'ABW',
'date_value': '2020-01-02',
'deaths': None,
'stringency': 0,
'stringency_actual': 0},
'AFG': {'confirmed': 0,
'country_code': 'AFG',
'date_value': '2020-01-02',
'deaths': 0,
'stringency': 0,
'stringency_actual': 0},
'AGO': {'confirmed': None,
'country_code': 'AGO',
'date_value': '2020-01-02',
'deaths': None,
'stringency': 0,
'stringency_actual': 0},
'AUS': {'confirmed': 0,
'country_code': 'AUS',
'date_value': '2020-01-02',
'deaths': 0,
'stringency': 7.14,
'stringency_actual': 7.14},
'AUT': {'confirmed': 0,
'country_code': 'AUT',
'date_value': '2020-01-02',
'deaths': 0,
'stringency': 0,
'stringency_actual': 0},.........
到目前为止,这是我的代码(为了这篇文章,我已经缩短了一点):
# create empty list for dates
date_index = []
[date_index.append(date) for date in stringency_data["data"]]
#creates empty lists for countries
Australia = []
Austria = []
...
US = []
# put these lists into a list
countries_lists = [Australia, Austria,...US]
# put country codes into a list
country_codes = ["AUS", "AUT",..."USA"]
# loop through countries
i = 0
for country, code in zip(countries_lists, country_codes):
while i<=len(date_index):
country.append(stringency_data["data"][date_index[i]][code]["stringency_actual"])
i+=1
当我打印“澳大利亚”列表时,我得到了我想要的所有值。但是从奥地利开始的任何国家仍然是一个空列表。
我得到输出 - KeyError:“AUS”。这表明代码检索了整个时间序列,但仅针对第一个国家(澳大利亚)。如何为每个国家/地区代码循环?
【问题讨论】:
-
我会调试/打印您要表达的列表的内容。尝试打印出 list(stringency_data["data"])。根据上面的代码示例,我看不出 stringency_data 中的“data”键在哪里有意义,但这可能会引导您朝着正确的方向前进,这可能是您想要从每个 stringency_data["data "].值()。你能告诉我们 stringency_data 是在哪里分配的,所以我们可以看到“data”键吗?
-
还请注意,您可以只分配列表理解的结果,而不是使用 append 对其进行迭代。例如。 myList = [thing.property for thing in myDict["myKey"]] 可能是您最终得到的结果。
-
KeyError: "AUS"建议在数据中找不到 AUS - 您是否检查过数据以确保 AUS 确实在其中?您可能想要在该块中捕获异常,或者使用.get()为未找到密钥提供安全默认值。 -
@XGoodrich 我已经编辑了帖子以显示我从哪里获得“数据”键并显示了严格性数据元素。 'data' 基本上是 json 文件的第一个键。
-
@Oliver.R 嘿奥利弗,我已经修改了帖子以表明澳大利亚确实在那里,关于如何进一步解决问题的想法?谢谢:)
标签: python json list loops for-loop