遍历从API数据接收到的嵌套JSON不起作用

时间:2018-07-13 22:16:13

标签: api iteration python-2.x

我尝试了每种方法,观看了每个视频,并从Stack Overflow寻求解决方案。似乎没有任何作用。我的目标是创建仅包含原始数据的列表。 理想输出的示例: [date1, data1a, data1b, data1c, data1d, data1f, date2, data2a, data2b, etc,]我花在一些本来应该是微不足道的事情上的时间使我非常沮丧。我必须缺少一些东西。

下面是检索后刚打印的数据。

{'2018-07-13': {'1. open': '1202.8000', '2. high': '1210.4400', '3. low': '1195.2900', '4. close': '1204.4200', '5. volume': '1630421'}, '2018-07-12': {'1. open': '1174.8600', '2. high': '1201.9900', '3. low': '1173.1000', '4. close': '1201.2600', '5. volume': '2203446'}, '2018-07-11': {'1. open': '1155.6200', '2. high': '1180.4400', '3. low': '1155.3700', '4. close': '1171.4600', '5. volume': '1662633'}, '2018-07-10': {'1. open': '1169.9900', '2. high': '1173.5000', '3. low': '1162.5600', '4. close': '1167.1400', '5. volume': '1066704'}, '2018-07-09': {'1. open': '1160.0000', '2. high': '1167.9300', '3. low': '1157.2300', '4. close': '1167.2800', '5. volume': '1079201'}, '2018-07-06': {'1. open': '1141.7700', '2. high': '1156.2000', '3. low': '1137.2400', '4. close': '1155.0800', '5. volume': '1090967'}, '2018-07-05': {'1. open': '1124.6000', '2. high': '1144.1400', '3. low': '1123.1200', '4. close': '1141.2900', '5. volume': '1429710'}, '2018-07-03': {'1. open': '1149.4200', '2. high': '1149.9100', '3. low': '1114.4300', '4. close': '1116.2800', '5. volume': '822394'}, '2018-07-02': {'1. open': '1115.3500', '2. high': '1142.9900', '3. low': '1106.6000', '4. close': '1142.1100', '5. volume': '1161040'}, '2018-06-29': {'1. open': '1132.3100', '2. high': '1141.3600', '3. low': '1127.0200', '4. close': '1129.1900', '5. volume': '1578116'}, '2018-06-28': {'1. open': '1112.3900', '2. high': '1134.0000', '3. low': '1106.0700', '4. close': '1126.7800', '5. volume': '1486675'}, '2018-06-27': {'1. open': '1136.0000', '2. high': '1146.2100', '3. low': '1116.7000', '4. close': '1116.9400', '5. volume': '1726866'}, '2018-06-26': {'1. open': '1144.1400', '2. high': '1146.8700', '3. low': '1129.0000', '4. close': '1132.6200', '5. volume': '1734414'}, '2018-06-25': {'1. open': '1155.0000', '2. high': '1155.9900', '3. low': '1119.9000', '4. close': '1139.2800', '5. volume': '2884131'}, '2018-06-22': {'1. open': '1171.4900', '2. high': '1175.0000', '3. low': '1159.6500', '4. close': '1169.2900', '5. volume': '1711003'}, '2018-06-21': {'1. open': '1185.5100', '2. high': '1190.3300', '3. low': '1163.4800', '4. close': '1169.4400', '5. volume': '2248277'}, '2018-06-20': {'1. open': '1183.3000', '2. high': '1201.4900', '3. low': '1182.0300', '4. close': '1184.0700', '5. volume': '2584260'}, '2018-06-19': {'1. open': '1170.1100', '2. high': '1182.8100', '3. low': '1161.6100', '4. close': '1178.6900', '5. volume': '2364127'}, '2018-06-18': {'1. open': '1152.6900', '2. high': '1184.1300', '3. low': '1151.0000', '4. close': '1183.5800', '5. volume': '1651913'}, '2018-06-15': {'1. open': '1159.9200', '2. high': '1163.9100', '3. low': '1153.6900', '4. close': '1159.2700', '5. volume': '2221891'}, '2018-06-14': {'1. open': '1152.2100', '2. high': '1165.9900', '3. low': '1150.6000', '4. close': '1160.1100', '5. volume': '1771120'}, '2018-06-13': {'1. open': '1152.2800', '2. high': '1155.6400', '3. low': '1143.3800', '4. close': '1144.2300', '5. volume': '1715074'}, '2018-06-12': {'1. open': '1141.0200', '2. high': '1148.7500', '3. low': '1141.0200', '4. close': '1148.1900', '5. volume': '1304612'}, '2018-06-11': {'1. open': '1132.9400', '2. high': '1147.7400', '3. low': '1131.4000', '4. close': '1140.9000', '5. volume': '1239770'}, '2018-06-08': {'1. open': '1131.2100', '2. high': '1138.7800', '3. low': '1123.2300', '4. close': '1132.7100', '5. volume': '1364226'}, '2018-06-07': {'1. open': '1144.5800', '2. high': '1145.8700', '3. low': '1126.1200', '4. close': '1134.4200', '5. volume': '1805388'}, '2018-06-06': {'1. open': '1152.7700', '2. high': '1154.7100', '3. low': '1136.3200', '4. close': '1146.9500', '5. volume': '1746719'}, '2018-06-05': {'1. open': '1154.6600', '2. high': '1161.1000', '3. low': '1147.4600', '4. close': '1151.0200', '5. volume': '1648222'}, '2018-06-04': {'1. open': '1138.5000', '2. high': '1157.8600', '3. low': '1137.0000', '4. close': '1153.0400', '5. volume': '2237701'}, '2018-06-01': {'1. open': '1112.8700', '2. high': '1138.1600', '3. low': '1112.0100', '4. close': '1135.0000', '5. volume': '3160100'}, '2018-05-31': {'1. open': '1082.0000', '2. high': '1110.0000', '3. low': '1078.0000', '4. close': '1100.0000', '5. volume': '3968362'}, '2018-05-30': {'1. open': '1073.4800', '2. high': '1078.9900', '3. low': '1066.5500', '4. close': '1077.4700', '5. volume': '1434282'}, '2018-05-29': {'1. open': '1076.0000', '2. high': '1081.3400', '3. low': '1063.0800', '4. close': '1068.0700', '5. volume': '1803805'}, '2018-05-25': {'1. open': '1086.5500', '2. high': '1089.5700', '3. low': '1082.6000', '4. close': '1084.0800', '5. volume': '1111161'}, '2018-05-24': {'1. open': '1086.9000', '2. high': '1087.1200', '3. low': '1072.3000', '4. close': '1085.4500', '5. volume': '1030194'}, '2018-05-23': {'1. open': '1070.0000', '2. high': '1088.0000', '3. low': '1066.9600', '4. close': '1085.9600', '5. volume': '1186998'}, '2018-05-22': {'1. open': '1089.8000', '2. high': '1091.8000', '3. low': '1072.2600', '4. close': '1075.3100', '5. volume': '1111299'}, '2018-05-21': {'1. open': '1079.0000', '2. high': '1093.3000', '3. low': '1078.0000', '4. close': '1084.0100', '5. volume': '1258999'}, '2018-05-18': {'1. open': '1066.0000', '2. high': '1073.7300', '3. low': '1064.6800', '4. close': '1069.6400', '5. volume': '1774149'}, '2018-05-17': {'1. open': '1081.4600', '2. high': '1091.0000', '3. low': '1076.4200', '4. close': '1081.2600', '5. volume': '1286907'}, '2018-05-16': {'1. open': '1085.0900', '2. high': '1094.3800', '3. low': '1081.6300', '4. close': '1084.0900', '5. volume': '1281406'}, '2018-05-15': {'1. open': '1096.9000', '2. high': '1099.1200', '3. low': '1078.7800', '4. close': '1084.8700', '5. volume': '1786926'}, '2018-05-14': {'1. open': '1105.5700', '2. high': '1118.1500', '3. low': '1104.8000', '4. close': '1106.6000', '5. volume': '1966893'}, '2018-05-11': {'1. open': '1100.4100', '2. high': '1109.0100', '3. low': '1097.2200', '4. close': '1103.3800', '5. volume': '1525218'}, '2018-05-10': {'1. open': '1095.0000', '2. high': '1109.4100', '3. low': '1093.8800', '4. close': '1105.4700', '5. volume': '1820676'}, '2018-05-09': {'1. open': '1064.1000', '2. high': '1094.0000', '3. low': '1062.1100', '4. close': '1088.9500', '5. volume': '2357979'}, '2018-05-08': {'1. open': '1064.6200', '2. high': '1065.2300', '3. low': '1051.6400', '4. close': '1058.5900', '5. volume': '1301524'}, '2018-05-07': {'1. open': '1053.8900', '2. high': '1065.0000', '3. low': '1050.2700', '4. close': '1059.4600', '5. volume': '1719971'}, '2018-05-04': {'1. open': '1019.6100', '2. high': '1051.8800', '3. low': '1018.0500', '4. close': '1051.0000', '5. volume': '1844600'}, '2018-05-03': {'1. open': '1025.3700', '2. high': '1030.6900', '3. low': '1007.8900', '4. close': '1026.3000', '5. volume': '1964066'}, '2018-05-02': {'1. open': '1034.0000', '2. high': '1043.9800', '3. low': '1023.1600', '4. close': '1026.0500', '5. volume': '1734872'}, '2018-05-01': {'1. open': '1016.3000', '2. high': '1041.7300', '3. low': '1010.6100', '4. close': '1040.7500', '5. volume': '1766256'}, '2018-04-30': {'1. open': '1034.4200', '2. high': '1038.3700', '3. low': '1018.3000', '4. close': '1018.5800', '5. volume': '1724632'}, '2018-04-27': {'1. open': '1045.5400', '2. high': '1051.6900', '3. low': '1027.5900', '4. close': '1031.4500', '5. volume': '2037263'}, '2018-04-26': {'1. open': '1033.2200', '2. high': '1052.0200', '3. low': '1020.4500', '4. close': '1043.3100', '5. volume': '2546332'}, '2018-04-25': {'1. open': '1029.7500', '2. high': '1036.0700', '3. low': '1017.0100', '4. close': '1022.9900', '5. volume': '2893047'}, '2018-04-24': {'1. open': '1059.2000', '2. high': '1064.4900', '3. low': '1012.8600', '4. close': '1022.6400', '5. volume': '6411038'}, '2018-04-23': {'1. open': '1082.9800', '2. high': '1088.0000', '3. low': '1066.6800', '4. close': '1073.8100', '5. volume': '3479496'}, '2018-04-20': {'1. open': '1084.2700', '2. high': '1094.7500', '3. low': '1072.1000', '4. close': '1077.3200', '5. volume': '2121692'}, '2018-04-19': {'1. open': '1069.0200', '2. high': '1097.5100', '3. low': '1069.0200', '4. close': '1089.4500', '5. volume': '1994247'}, '2018-04-18': {'1. open': '1079.0100', '2. high': '1082.0000', '3. low': '1070.5200', '4. close': '1075.3900', '5. volume': '1556298'}, '2018-04-17': {'1. open': '1061.2000', '2. high': '1085.0000', '3. low': '1057.0900', '4. close': '1079.3600', '5. volume': '2729197'}, '2018-04-16': {'1. open': '1045.5500', '2. high': '1051.4300', '3. low': '1033.9200', '4. close': '1046.1000', '5. volume': '1488822'}, '2018-04-13': {'1. open': '1046.8900', '2. high': '1052.9800', '3. low': '1030.1500', '4. close': '1036.0400', '5. volume': '1270461'}, '2018-04-12': {'1. open': '1031.4700', '2. high': '1044.6500', '3. low': '1026.0500', '4. close': '1037.2900', '5. volume': '1644794'}, '2018-04-11': {'1. open': '1032.0000', '2. high': '1035.8700', '3. low': '1019.2000', '4. close': '1025.0600', '5. volume': '1703015'}, '2018-04-10': {'1. open': '1030.2600', '2. high': '1041.2100', '3. low': '1015.4100', '4. close': '1036.5000', '5. volume': '1836678'}, '2018-04-09': {'1. open': '1020.0400', '2. high': '1044.5900', '3. low': '1019.2000', '4. close': '1020.0900', '5. volume': '1661012'}, '2018-04-06': {'1. open': '1023.1000', '2. high': '1036.0000', '3. low': '1006.2500', '4. close': '1009.9500', '5. volume': '2006683'}, '2018-04-05': {'1. open': '1046.3900', '2. high': '1046.7100', '3. low': '1024.0000', '4. close': '1032.6400', '5. volume': '1791761'}, '2018-04-04': {'1. open': '998.2300', '2. high': '1032.7400', '3. low': '996.5100', '4. close': '1029.7100', '5. volume': '2536198'}, '2018-04-03': {'1. open': '1016.1500', '2. high': '1025.0000', '3. low': '997.2450', '4. close': '1018.6800', '5. volume': '2566146'}, '2018-04-02': {'1. open': '1027.6200', '2. high': '1039.5300', '3. low': '994.2500', '4. close': '1012.6300', '5. volume': '3253387'}, '2018-03-29': {'1. open': '1011.2100', '2. high': '1048.0500', '3. low': '1004.0400', '4. close': '1037.1400', '5. volume': '3466876'}, '2018-03-28': {'1. open': '1001.9100', '2. high': '1024.9900', '3. low': '984.0000', '4. close': '1005.1800', '5. volume': '3884943'}, '2018-03-27': {'1. open': '1063.9000', '2. high': '1064.5400', '3. low': '997.6200', '4. close': '1006.9400', '5. volume': '2940957'}, '2018-03-26': {'1. open': '1050.6000', '2. high': '1059.2700', '3. low': '1010.5800', '4. close': '1054.0900', '5. volume': '3272409'}, '2018-03-23': {'1. open': '1051.3700', '2. high': '1066.7800', '3. low': '1024.8700', '4. close': '1026.5500', '5. volume': '2413517'}, '2018-03-22': {'1. open': '1080.0100', '2. high': '1083.9200', '3. low': '1049.6400', '4. close': '1053.1500', '5. volume': '3418154'}, '2018-03-21': {'1. open': '1092.5700', '2. high': '1108.7000', '3. low': '1087.2100', '4. close': '1094.0000', '5. volume': '1990515'}, '2018-03-20': {'1. open': '1098.4000', '2. high': '1105.5500', '3. low': '1082.4200', '4. close': '1095.8000', '5. volume': '2709310'}, '2018-03-19': {'1. open': '1117.7600', '2. high': '1119.3700', '3. low': '1088.9200', '4. close': '1100.0700', '5. volume': '3076349'}, '2018-03-16': {'1. open': '1155.3500', '2. high': '1156.8100', '3. low': '1131.3600', '4. close': '1134.4200', '5. volume': '2654602'}, '2018-03-15': {'1. open': '1149.5700', '2. high': '1162.5000', '3. low': '1135.6600', '4. close': '1150.6100', '5. volume': '1623868'}, '2018-03-14': {'1. open': '1145.8000', '2. high': '1159.7600', '3. low': '1142.3500', '4. close': '1148.8900', '5. volume': '2033697'}, '2018-03-13': {'1. open': '1171.8300', '2. high': '1178.0000', '3. low': '1134.5700', '4. close': '1139.9100', '5. volume': '2129435'}, '2018-03-12': {'1. open': '1165.0500', '2. high': '1178.1600', '3. low': '1159.2000', '4. close': '1165.9300', '5. volume': '2129297'}, '2018-03-09': {'1. open': '1139.5000', '2. high': '1161.0000', '3. low': '1134.2900', '4. close': '1160.8400', '5. volume': '2070174'}, '2018-03-08': {'1. open': '1117.2000', '2. high': '1131.4400', '3. low': '1117.2000', '4. close': '1129.3800', '5. volume': '1510478'}, '2018-03-07': {'1. open': '1092.8200', '2. high': '1116.2000', '3. low': '1089.9100', '4. close': '1115.0400', '5. volume': '1537429'}, '2018-03-06': {'1. open': '1102.1000', '2. high': '1105.6300', '3. low': '1094.5000', '4. close': '1100.9000', '5. volume': '1169068'}, '2018-03-05': {'1. open': '1078.1300', '2. high': '1101.1800', '3. low': '1072.2700', '4. close': '1094.7600', '5. volume': '1432369'}, '2018-03-02': {'1. open': '1057.9800', '2. high': '1086.8900', '3. low': '1050.1100', '4. close': '1084.1400', '5. volume': '2508145'}, '2018-03-01': {'1. open': '1109.5400', '2. high': '1111.2700', '3. low': '1067.2900', '4. close': '1071.4100', '5. volume': '2766856'}, '2018-02-28': {'1. open': '1122.0000', '2. high': '1127.6500', '3. low': '1103.0000', '4. close': '1103.9200', '5. volume': '2431023'}, '2018-02-27': {'1. open': '1143.7000', '2. high': '1144.2500', '3. low': '1116.7900', '4. close': '1117.5100', '5. volume': '2094863'}, '2018-02-26': {'1. open': '1131.8600', '2. high': '1144.2000', '3. low': '1129.3400', '4. close': '1143.7000', '5. volume': '1489118'}, '2018-02-23': {'1. open': '1118.6600', '2. high': '1129.0000', '3. low': '1108.4400', '4. close': '1128.0900', '5. volume': '1234539'}, '2018-02-22': {'1. open': '1119.1700', '2. high': '1125.4600', '3. low': '1105.1500', '4. close': '1109.9000', '5. volume': '1386115'}, '2018-02-21': {'1. open': '1109.1000', '2. high': '1136.2000', '3. low': '1107.5100', '4. close': '1113.7500', '5. volume': '2024534'}}

我正在使用的代码在下面,我在某些部分进行了注释,因为我尝试了许多不同的事情,这些注释指的是收到的错误以及为什么将代码更改为现在的代码。

>
#a wrapper I installed to help deal with the Alpha Vantage API, originally I coded everything myself but, after 
#realising that this worked for many others I thought I would limit the possible sources of errors that could
#come from me to help problem solve.
from alpha_vantage.timeseries import TimeSeries
ts = TimeSeries(key='*************')
# Get json object with the intraday data and another with  the call's metadata
data, meta_data = ts.get_daily('GOOGL')

print(data) #This works fine and prints the data in the expected format

print(type(data)) #This prints out "<class 'dict'>".

data_list = []
for date in data: # This will succesfully print out all of the dates(not broken up) if i leave out the below for
                  #  loop (which inforces the idea that each key is an attribute *pointing* towards information.
    for info in date: #When I try the "date.get" method, it states that "'str' object has no attribute 'get'.
        print(date[0])  #When I change it to date['1 open'] etc., it tells me that it must be an interger.
        print(date[1])  #When I use numbers it literally iterating through the name of the key as though
        print(date[2])  #it where a string. To out put one character at a time (and not even of the info I want to get).
        print(date[3])
        print(date[4])

循环的当前输出:

2
0
1
8
-
2
0
1
8
-
2
0
1
8
-
2
0
1
8
-
2
0
1
8
-
2
0
1
8

这一直持续到数据不再包含日期键为止。

1 个答案:

答案 0 :(得分:2)

您在这里面临两个问题:

问题#1 是您正在解析的数据实际上不是键:值对象对,正如您的代码所暗示的那样;它实际上是一个元组数组。为什么? Dunno-肯定不是最优雅的API设计。

问题2 是因为this issue, which is solved by using .iteritems() (or .items() for Python 3)

尝试一下:

for o in data.iteritems():
    for date, data in {o[0]: o[1]}.iteritems():
        print(date, data['1. open'], data['2. high'], ....etc.)