首先感谢您的帮助。我对在 stackoverflow 上发布问题非常陌生。如果我做得太困惑或没有遵循有效的格式,请告诉我。
我首先使用 BLS 网站上的示例 Python 代码,但当我尝试运行它时它不起作用。就是下面的代码。
import requests
import json
import prettytable
headers = {'Content-type': 'application/json'}
data = json.dumps({"seriesid": ['CUUR0000SA0','SUUR0000SA0'],"startyear":"2011", "endyear":"2014"})
p = requests.post('http://api.bls.gov/publicAPI/v1/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("c:\\temp\\" + seriesId + ".txt","w")
output.write (x.get_string())
output.close()
它给出了以下错误。
File "C:\Users\Benjamin\Documents\Website\Python\BLS Second.py", line 20
'if 'M01' <= period <= 'M12':'
^
SyntaxError: invalid syntax
[Finished in 0.1s with exit code 1]
之后我将其修改为下面的代码,这样我至少可以检索数据。
import json
import prettytable
import xlwt
import requests
headers = {"Content-Type": "application/json"}
data = json.dumps({"seriesid": ["LAUDV061124400000003"], "startyear": "2010", "endyear": "2015"})
p = requests.post("http://api.bls.gov/publicAPI/v1/timeseries/data/"data = data, headers = headers)
json_data = json.loads(p.text)
然后我尝试使用以下方法将该结果放入 Excel 电子表格中:
workbook = xlwt.Workbook(encoding="utf-8")
sheet1 = workbook.add_sheet("Python Sheet1")
sheet1.write(0,0,p.text)
workbook.save("Pythonspreadsheet1.xls")
print ("Workbook Created")
它可以工作,但它将下面的所有 JSON 放入一个单元格中。
{“状态”:“REQUEST_SUCCEEDED”,“响应时间”:321,“消息”:[],“结果”:{ “系列”: [{"seriesID":"LAUDV061124400000003","data":[{"year":"2014","period":"M12","periodName":"12月","value":"4.6","脚注":[{"code":"R","text":"数据更新日期为2016年4月15日。"}]}, {"year":"2014","period":"M11", "periodName":"11 月","value":"5.1","footnotes":[{"code":"R","text":"数据于 2016 年 4 月 15 日进行修订。"}]} ,{"year":"2014","period":"M10","periodName":"10 月","value":"5.2","footnotes":[{"code":"R","text ":"数据于2016年4月15日进行修订。"}]},{"year":"2014","period":"M09","periodName":"9月","value":"5.2 ","footnotes":[{"code":"R","text":"数据于2016年4月15日进行修订。"}]},{"year":"2014","period": "M08","periodName":"8月","value":"5.8","footnotes":[{"code":"R","text":"数据于4月15日进行修订,
如何将数据拆分为包含 Year、periodName 和 Value 数据的单独列,而不使用所有 JSON 括号和引号?
我希望它在 Excel 中看起来像下面这样,每条数据都在自己的单元格中:
Year periodName Value
2014 January 254.3
2014 February 356.8
2014 March 456.5
2014 April 422.3
2014 May 415.8
再次感谢您的帮助。
最佳答案
添加了一个完全可重现的示例,说明如何从劳工统计局 ( BLS ) API 获取数据。他们的文档 ( link here ) 提供了如何使用 python 从 API 获取数据的示例。
在下面的代码中,我使用一个函数创建一个数据帧,其中还添加了 try 和 except 子句,以便在用户超过 BLS API 的每日点击次数时引发异常(未注册用户每天最多可以请求 25 个查询) -per documentation FAQs)。最后,使用 pandas.DataFrame.to_excel()
创建一个 Excel 文件。
我使用 OP 的 BLS 系列和开始年份作为示例。
import pandas as pd
import json
import requests
def get_bls_data(series, start, end):
headers = {'Content-Type': 'application/json'}
data = json.dumps({"seriesid": series,"startyear":"%d" % (start), "endyear":"%d" % (end)})
p = requests.post('https://api.bls.gov/publicAPI/v1/timeseries/data/', data=data, headers=headers)
json_data = json.loads(p.text)
try:
df = pd.DataFrame()
for series in json_data['Results']['series']:
df_initial = pd.DataFrame(series)
series_col = df_initial['seriesID'][0]
for i in range(0, len(df_initial) - 1):
df_row = pd.DataFrame(df_initial['data'][i])
df_row['seriesID'] = series_col
if 'code' not in str(df_row['footnotes']):
df_row['footnotes'] = ''
else:
df_row['footnotes'] = str(df_row['footnotes']).split("'code': '",1)[1][:1]
df = df.append(df_row, ignore_index=True)
return df
except:
json_data['status'] == 'REQUEST_NOT_PROCESSED'
print('BLS API has given the following Response:', json_data['status'])
print('Reason:', json_data['message'])
start = 2014
end = 2018
series = ['LAUDV061124400000003']
df = get_bls_data(series=series, start=start, end=end)
writer = pd.ExcelWriter('bls.xlsx', engine='xlsxwriter', options={'strings_to_numbers': True})
df.to_excel(writer, sheet_name='Sheet1', index=False)
writer.save()
bls.xlsx 的预期输出:
关于python - 使用 Python 将 BLS API 数据输入 Microsoft Excel,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36704698/