others - python - 當key不規則時,如何從JSON中提取數據?

通常可以使用for循環遍歷鍵,我怎樣才能訪問所有的數據?


{


"2020-02-06": {


"1. open":"699.9200",


"2. high":"795.8300",


"3. low":"687.0000",


"4. close":"748.9600",


"5. volume":"39647744"


 },


"2020-02-05": {


"1. open":"823.2600",


"2. high":"845.9800",


"3. low":"704.1106",


"4. close":"734.7000",


"5. volume":"48423837"


 },


"2020-02-04": {


"1. open":"882.9600",


"2. high":"968.9899",


"3. low":"833.8800",


"4. close":"887.0600",


"5. volume":"60938758"


 },


"2020-02-03": {


"1. open":"673.6900",


"2. high":"786.1400",


"3. low":"673.5200",


"4. close":"780.0000",


"5. volume":"47233495"


 },



时间:

你可以這樣循環


for d, quotes in data.items():


 print('Quotes for day {} are {}'.format(d, quotes))



循環dict中的每個鍵


open_quotes = [data[x]["1. open"] for x in data]



你可以這樣做,假設你的字典是d


l = []


for i in d.items():


 l.append(i[1]["1. open"])




>>> d = {


"2020-02-06": {


"1. open":"699.9200",


"2. high":"795.8300",


"3. low":"687.0000",


"4. close":"748.9600",


"5. volume":"39647744"


 },


"2020-02-05": {


"1. open":"823.2600",


"2. high":"845.9800",


"3. low":"704.1106",


"4. close":"734.7000",


"5. volume":"48423837"


 },


"2020-02-04": {


"1. open":"882.9600",


"2. high":"968.9899",


"3. low":"833.8800",


"4. close":"887.0600",


"5. volume":"60938758"


 },


"2020-02-03": {


"1. open":"673.6900",


"2. high":"786.1400",


"3. low":"673.5200",


"4. close":"780.0000",


"5. volume":"47233495"


 }}



>>> open_list = [d[date]["1. open"] for date in d.keys()]


>>> open_list


['699.9200', '823.2600', '882.9600', '673.6900']




...