[英]How do I fix 'KeyError: 'series' in BLS API sample Python code?
I'm trying to replicate the "API Version 2.0 Python Sample Code" provided by the Bureau of Labor Statistics (BLS) on its website here , but receive "KeyError: 'series'" whenever I try to run it.我正在尝试复制劳工统计局 (BLS) 在其网站上提供的“API 版本 2.0 Python 示例代码”,但每当我尝试运行它时都会收到“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()
Here is the output:这是 output:
---------------------------------------------------------------------------
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'
I don't understand the cause of this error.我不明白这个错误的原因。 Will you please help me figure it out and fix it?
你能帮我弄清楚并修复它吗? Many thanks!
非常感谢!
the code works for me, this is the output:该代码对我有用,这是 output:
+-------------+------+--------+---------+-----------+
| 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 | |
+-------------+------+--------+---------+-----------+
can you try to run the code inside your terminal insead of using ipython?您可以尝试在终端内运行代码而不是使用 ipython 吗?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.