Reputation: 698
How can I print/access just the dates of "Time Series (Daily)"?
I tried the following, but I get a key error:
for date in range(0,2):
print(date)
print(tickerData['Time Series (Daily)'][date])
Here is the nested dictionary:
{
"Meta Data": {
"1. Information": "Daily Prices (open, high, low, close) and Volumes",
"2. Symbol": "NASDAQ Index",
"3. Last Refreshed": "2018-04-13",
"4. Output Size": "Compact",
"5. Time Zone": "US/Eastern"
},
"Time Series (Daily)": {
"2018-04-13": {
"1. open": "7179.6201",
"2. high": "7183.6201",
"3. low": "7078.1401",
"4. close": "7106.6499",
"5. volume": "1743640000"
},
"2018-04-12": {
"1. open": "7112.0200",
"2. high": "7166.0000",
"3. low": "7105.0898",
"4. close": "7140.2500",
"5. volume": "2021110000"
}}}
Upvotes: 1
Views: 45
Reputation: 1340
You can use below to get all dates in your dictionary:
dates = tickerdata["Time Series (Daily)"]
for item in dates:
print (item)
Output:
2018-04-13
2018-04-12
For Comment below:
Not sure if this is what you want but you can do something like below:
class AttributeDict(dict):
__getattr__ = dict.__getitem__
__setattr__ = dict.__setitem__
tickerdata = {
"Meta Data": {
"1. Information": "Daily Prices (open, high, low, close) and Volumes",
"2. Symbol": "NASDAQ Index",
"3. Last Refreshed": "2018-04-13",
"4. Output Size": "Compact",
"5. Time Zone": "US/Eastern"
},
"TimeSeriesDaily": {
"2018-04-13": {
"1. open": "7179.6201",
"2. high": "7183.6201",
"3. low": "7078.1401",
"4. close": "7106.6499",
"5. volume": "1743640000"
},
"2018-04-12": {
"1. open": "7112.0200",
"2. high": "7166.0000",
"3. low": "7105.0898",
"4. close": "7140.2500",
"5. volume": "2021110000"
}}}
tmp = AttributeDict(tickerdata)
for date in tmp.'TimeSeriesDaily':
print(date)
but if you notice for this your 'Time Series (Daily)' label need to be simplified, which I don't think you may have control or want to do.
Upvotes: 1
Reputation: 4213
tickerData ={
"Meta Data": {
"1. Information": "Daily Prices (open, high, low, close) and Volumes",
"2. Symbol": "NASDAQ Index",
"3. Last Refreshed": "2018-04-13",
"4. Output Size": "Compact",
"5. Time Zone": "US/Eastern"
},
"Time Series (Daily)": {
"2018-04-13": {
"1. open": "7179.6201",
"2. high": "7183.6201",
"3. low": "7078.1401",
"4. close": "7106.6499",
"5. volume": "1743640000"
},
"2018-04-12": {
"1. open": "7112.0200",
"2. high": "7166.0000",
"3. low": "7105.0898",
"4. close": "7140.2500",
"5. volume": "2021110000"
}
}
}
for date in tickerData['Time Series (Daily)'].keys():
print(date)
Upvotes: 2