问题描述
我最近从AlphaVantage.co获得了一个API密钥,以读取时间序列数据(股票)。我能够编写请求和将JSON文件存储到Python字典中的Python代码,
stockinfo = requests.get(full_url).json()
但是我无法弄清楚如何生成从生成的嵌套Python字典中读取的代码,因为名称随请求日期的变化而变化。下面是JSON文件的截断副本。
我是Python的新手,但熟悉如何阅读字典
closing_price = stockinfo["Time Series (Daily)"]["2020-09-10"]["4. close"]
但是嵌套字典键的更改是不断变化的日期。
谢谢您的帮助。
{
"Meta Data": {
"1. information": "Daily Prices (open,high,low,close) and Volumes","2. Symbol": "MSFT","3. Last Refreshed": "2020-09-10","4. Output Size": "Full size","5. Time Zone": "US/Eastern"
},"Time Series (Daily)": {
"2020-09-10": {
"1. open": "213.4000","2. high": "214.7400","3. low": "204.1100","4. close": "205.3700","5. volume": "35461514"
},"2020-09-09": {
"1. open": "207.6000","2. high": "214.8399","3. low": "206.7000","4. close": "211.2900","5. volume": "45678986"
},"1999-11-02": {
"1. open": "92.7500","2. high": "94.5000","3. low": "91.9400","4. close": "92.5600","5. volume": "23174500"
},"1999-11-01": {
"1. open": "93.2500","2. high": "94.1900","3. low": "92.1200","4. close": "92.3700","5. volume": "26630600"
}
}
}
解决方法
我认为您想要这样的东西:
`> dput(head(sample))
structure(list(ESIR_SQ001 = c(NA,"A4","A3",NA),ESIR_SQ002 = c(NA,"A2",ESIR_SQ003 = c(NA,ESIR_SQ004 =
c(NA,"A1",NA)),class = c("tbl_df","tbl","data.frame"
),row.names = c(NA,-6L))`