我正在尝试将劳工统计局提供的“API2.0Python Sample Code”复制到its website here上,但每当我尝试运行它时,都会收到"KeyError:'series'“。
import requests
import json
import prettytable
headers = {'Content-type': 'application/json'}
data = json.dumps({"seriesid": ['CUUR0000SA0','SUUR0000SA0'],"startyear":"2011", "endyear":"2014"})
p = requests.post('https://api.bls.gov/publicAPI/v2/timeseries/data/', data=data, headers=headers)
json_data = json.loads(p.text)
for series in json_data['Results']['series']:
x=prettytable.PrettyTable(["series id","year","period","value","footnotes"])
seriesId = series['seriesID']
for item in series['data']:
year = item['year']
period = item['period']
value = item['value']
footnotes=""
for footnote in item['footnotes']:
if footnote:
footnotes = footnotes + footnote['text'] + ','
if 'M01' <= period <= 'M12':
x.add_row([seriesId,year,period,value,footnotes[0:-1]])
output = open(seriesId + '.txt','w')
output.write (x.get_string())
output.close()下面是输出:
---------------------------------------------------------------------------
KeyError Traceback (most recent call last)
<ipython-input-21-b89f58b9b664> in <module>
3 p = requests.post('https://api.bls.gov/publicAPI/v2/timeseries/data/', data=data, headers=headers)
4 json_data = json.loads(p.text)
----> 5 for series in json_data['Results']['series']:
6 x=prettytable.PrettyTable(["series id","year","period","value","footnotes"])
7 seriesId = series['seriesID']
KeyError: 'series'我不明白这个错误的原因。你能帮我弄清楚并修复它吗?非常感谢!
发布于 2021-03-31 22:37:57
代码适用于我,这是输出:
+-------------+------+--------+---------+-----------+
| series id | year | period | value | footnotes |
+-------------+------+--------+---------+-----------+
| SUUR0000SA0 | 2014 | M12 | 134.207 | |
| SUUR0000SA0 | 2014 | M11 | 135.107 | |
| SUUR0000SA0 | 2014 | M10 | 135.891 | |
| SUUR0000SA0 | 2014 | M09 | 136.211 | |
| SUUR0000SA0 | 2014 | M08 | 136.127 | |
| SUUR0000SA0 | 2014 | M07 | 136.392 | |
| SUUR0000SA0 | 2014 | M06 | 136.433 | |
| SUUR0000SA0 | 2014 | M05 | 136.216 | |
| SUUR0000SA0 | 2014 | M04 | 135.771 | |
| SUUR0000SA0 | 2014 | M03 | 135.375 | |
| SUUR0000SA0 | 2014 | M02 | 134.542 | |
| SUUR0000SA0 | 2014 | M01 | 134.017 | |
| SUUR0000SA0 | 2013 | M12 | 133.509 | |
| SUUR0000SA0 | 2013 | M11 | 133.596 | |
| SUUR0000SA0 | 2013 | M10 | 133.876 | |
| SUUR0000SA0 | 2013 | M09 | 134.255 | |
| SUUR0000SA0 | 2013 | M08 | 134.098 | |
| SUUR0000SA0 | 2013 | M07 | 133.919 | |
| SUUR0000SA0 | 2013 | M06 | 133.900 | |
| SUUR0000SA0 | 2013 | M05 | 133.626 | |
| SUUR0000SA0 | 2013 | M04 | 133.421 | |
| SUUR0000SA0 | 2013 | M03 | 133.558 | |
| SUUR0000SA0 | 2013 | M02 | 133.204 | |
| SUUR0000SA0 | 2013 | M01 | 132.137 | |
| SUUR0000SA0 | 2012 | M12 | 131.770 | |
| SUUR0000SA0 | 2012 | M11 | 132.208 | |
| SUUR0000SA0 | 2012 | M10 | 132.892 | |
| SUUR0000SA0 | 2012 | M09 | 132.988 | |
| SUUR0000SA0 | 2012 | M08 | 132.430 | |
| SUUR0000SA0 | 2012 | M07 | 131.731 | |
| SUUR0000SA0 | 2012 | M06 | 131.956 | |
| SUUR0000SA0 | 2012 | M05 | 132.154 | |
| SUUR0000SA0 | 2012 | M04 | 132.284 | |
| SUUR0000SA0 | 2012 | M03 | 131.905 | |
| SUUR0000SA0 | 2012 | M02 | 130.953 | |
| SUUR0000SA0 | 2012 | M01 | 130.438 | |
| SUUR0000SA0 | 2011 | M12 | 129.844 | |
| SUUR0000SA0 | 2011 | M11 | 130.196 | |
| SUUR0000SA0 | 2011 | M10 | 130.373 | |
| SUUR0000SA0 | 2011 | M09 | 130.635 | |
| SUUR0000SA0 | 2011 | M08 | 130.351 | |
| SUUR0000SA0 | 2011 | M07 | 129.983 | |
| SUUR0000SA0 | 2011 | M06 | 129.846 | |
| SUUR0000SA0 | 2011 | M05 | 129.999 | |
| SUUR0000SA0 | 2011 | M04 | 129.483 | |
| SUUR0000SA0 | 2011 | M03 | 128.585 | |
| SUUR0000SA0 | 2011 | M02 | 127.363 | |
| SUUR0000SA0 | 2011 | M01 | 126.778 | |
+-------------+------+--------+---------+-----------+你能尝试使用ipython在终端中运行代码吗?
https://stackoverflow.com/questions/66889198
复制相似问题