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']




...