I am learning to extract data from Bureau of Labor Statistics using their API. The sample code:
import requests
import json
import prettytable
headers = {'Content-type': 'application/json'}
data = json.dumps({"seriesid": ['LAUMT421090000000005'],"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()
I simply changed the seriesID to get the data I wanted. And the output the code generated is a text file named after the Series ID.
The data extracted is shown as:
A fraction of the actual text of the data:
LAUMT421090000000005 | 2014 | M12 | 405757 | Data were subject to revision on April 20, 2018. | | LAUMT421090000000005 | 2014 | M11 | 406061 | Data were subject to revision on April 20, 2018. | | LAUMT421090000000005 | 2014 | M10 | 405358 | Data were subject to revision on April 20, 2018. | | LAUMT421090000000005 | 2014 | M09 | 402164 | Data were subject to revision on April 20, 2018. | | LAUMT421090000000005 | 2014 | M08 | 400534 | Data were subject to revision on April 20, 2018.
Being a total novice in using Python and API, changing part of the source code to get the desirable data is the most I can achieve now. Due to work reasons, I need to present the data in Excel (I know I will be frowned upon, but this is how it is). However, Excel doesn't recognize "|" as the delimiter.
The sample code uses the prettytable library to generate the output data. I'm wondering if there are other ways to extract the data so that the result is easier to process or convert to comma separated values.
Thanks.