I recently acquired an API key from AlphaVantage.co to read time series data (stock). I was able to write Python code that requests and stores the JSON file into a Python dictionary,
stockinfo = requests.get(full_url).json()
but I am unable to figure out how to generate code that reads from the resulting nested Python dictionary because the names change as a function of the date of the request. Below is a truncated copy of the JSON file.
I am new to Python, but am familiar with how to read dictionaries
closing_price = stockinfo["Time Series (Daily)"]["2020-09-10"]["4. close"]
but the nested dictionary keys change are evolving dates.
Thank you for any assistance.
{
"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"
}
}
}
I think you want something like this:
for day_date, day_data in closing_price['Time Series (Daily)'].items():
print(f"Data for {day_date} is {day_data}")
print(f"High for {day_date} is {day_data['2. high']}")
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.