oshkoshbagoshh

BLS_API_SampleCode

Nov 24th, 2017
590
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.96 KB | None | 0 0
  1. import requests
  2. import json
  3. import prettytable
  4. headers = {'Content-type': 'application/json'}
  5. data = json.dumps({"seriesid": ['CUUR0000SA0','SUUR0000SA0'],"startyear":"2011", "endyear":"2014"})
  6. p = requests.post('https://api.bls.gov/publicAPI/v2/timeseries/data/', data=data, headers=headers)
  7. json_data = json.loads(p.text)
  8. for series in json_data['Results']['series']:
  9.     x=prettytable.PrettyTable(["series id","year","period","value","footnotes"])
  10.     seriesId = series['seriesID']
  11.     for item in series['data']:
  12.         year = item['year']
  13.         period = item['period']
  14.         value = item['value']
  15.         footnotes=""
  16.         for footnote in item['footnotes']:
  17.             if footnote:
  18.                 footnotes = footnotes + footnote['text'] + ','
  19.        'if 'M01' <= period <= 'M12':'
  20.             x.add_row([seriesId,year,period,value,footnotes[0:-1]])
  21.     output = open(seriesId + '.txt','w')
  22.     output.write (x.get_string())
  23.     output.close()
Advertisement
Add Comment
Please, Sign In to add comment