[英]Using Python to Input BLS API Data into Microsoft Excel
First off, thank you for helping.首先,谢谢你的帮助。 I am very new at posting questions on stackoverflow.我很擅长在 stackoverflow 上发布问题。 Please let me know if I made it too confusing or didn't follow an efficient format.如果我让它太混乱或没有遵循有效的格式,请告诉我。
I started by using the example Python code on the BLS website, but it doesn't work when I try to run it.我开始使用 BLS 网站上的示例 Python 代码,但是当我尝试运行它时它不起作用。 It is the code below.这是下面的代码。
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()
It gives the following error.它给出了以下错误。
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]
After that I modified it to the code below, so that I could at least retrieve data.之后我将其修改为下面的代码,以便我至少可以检索数据。
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)
Then I tried to put that result into an Excel spreadsheet using:然后我尝试使用以下方法将该结果放入 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")
It works, but it places all of the JSON below into one cell.它可以工作,但它将下面的所有 JSON 放入一个单元格中。
{"status":"REQUEST_SUCCEEDED","responseTime":321,"message":[],"Results":{ "series": [{"seriesID":"LAUDV061124400000003","data":[{"year":"2014","period":"M12","periodName":"December","value":"4.6","footnotes":[{"code":"R","text":"Data were subject to revision on April 15, 2016."}]}, {"year":"2014","period":"M11","periodName":"November","value":"5.1","footnotes":[{"code":"R","text":"Data were subject to revision on April 15, 2016."}]},{"year":"2014","period":"M10","periodName":"October","value":"5.2","footnotes":[{"code":"R","text":"Data were subject to revision on April 15, 2016."}]},{"year":"2014","period":"M09","periodName":"September","value":"5.2","footnotes":[{"code":"R","text":"Data were subject to revision on April 15, 2016."}]},{"year":"2014","period":"M08","periodName":"August","value":"5.8","footnotes":[{"code":"R","text":"Data were subject to revision on April 15, {"status":"REQUEST_SUCCEEDED","responseTime":321,"message":[],"Results":{ "series": [{"seriesID":"LAUDV061124400000003","data":[{"year" :"2014","period":"M12","periodName":"December","value":"4.6","footnotes":[{"code":"R","text":"数据分别为2016 年 4 月 15 日修订。"}]}, {"year":"2014","period":"M11","periodName":"November","value":"5.1","footnotes" :[{"code":"R","text":"数据于 2016 年 4 月 15 日修订。"}]},{"year":"2014","period":"M10"," periodName":"October","value":"5.2","footnotes":[{"code":"R","text":"数据于 2016 年 4 月 15 日修订。"}]}, {"year":"2014","period":"M09","periodName":"September","value":"5.2","footnotes":[{"code":"R","text" :"数据于 2016 年 4 月 15 日修订。"}]},{"year":"2014","period":"M08","periodName":"August","value":"5.8" ,"footnotes":[{"code":"R","text":"数据于4月15日修订,
How do I split the data up to have separate columns featuring Year, periodName, and Value data without all of the JSON brackets and quotes?如何将数据拆分为包含 Year、periodName 和 Value 数据的单独列,而没有所有 JSON 括号和引号?
I want it to look like the below in Excel with each piece of data in its own cell:我希望它在 Excel 中看起来像下面这样,每条数据都在自己的单元格中:
Year periodName Value
2014 January 254.3
2014 February 356.8
2014 March 456.5
2014 April 422.3
2014 May 415.8
Thank you again for helping.再次感谢您的帮助。
Adding a fully reproducible example of how to get data from the Bureau of Labor Statistics ( BLS ) API.添加一个完全可重现的示例,说明如何从劳工统计局 ( BLS ) API 获取数据。 Their Documentation ( link here ) provides an example of how to get data from the API using python.他们的文档(链接在这里)提供了一个如何使用 python 从 API 获取数据的示例。
In my code below, I create a dataframe using a function where I also add try and except clauses to raise an exception if the user exceeds the number of daily hits to the BLS API (Unregistered users may request up to 25 queries daily - per documentation FAQs ).在我下面的代码中,我使用一个函数创建了一个数据框,其中我还添加了 try 和 except 子句以在用户超过每天对 BLS API 的点击次数时引发异常(未注册用户每天最多可以请求 25 个查询 -每个文档常见问题解答)。 Finally, using pandas.DataFrame.to_excel()
I create an excel file.最后,使用pandas.DataFrame.to_excel()
我创建了一个 excel 文件。
I use the OP's BLS series and start year for my example.我使用 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()
Expected Output of bls.xlsx: bls.xlsx 的预期输出:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.