简体   繁体   中英

Download REST API JSON response to a CSV file

I'm incredibly new to Python/coding so I do apologise if this is a simple question.

I'm trying to download a REST API response to a CSV file

import requests
import json
import csv
from pprint import pprint
r = requests.get('https://finnhub.io/api/v1/stock/executive?symbol=AAPL&token=APIKEYHERE')
f = open ('output.csv', 'w')
writer = csv.writer(f)
for line in r.iter_lines():
    writer.writerow(line.decode('utf-8').split(','))
exit()

The problem is my response is this:

CSV File

{"executive":[{"age":68 "name":"Arthur D. Levinson" "since":2011 "title":"Independent Chairman of the Board"} {"age":58 "name":"Timothy D. Cook" "since":2011 "title":"Chief Executive Officer Director"} {"age":55 "name":"Luca Maestri" "since":2014 "title":"Chief Financial Officer Senior Vice President"} {"age":55 "name":"Jeffrey E. Williams" "since":2018 "title":"Chief Operating Officer"} {"age":53 "name":"Katherine L. Adams" "since":2017 "title":"Senior Vice President General Counsel Secretary"} {"age":58 "name":"Philip W. Schiller" "since":2002 "title":"Senior Vice President - Worldwide Marketing"} {"age":55 "name":"Eddy Cue" "since":2011 "title":"Senior Vice President - Internet Software and Services"} {"age":48 "name":"Craig Federighi" "since":2012 "title":"Senior Vice President - Software Engineering"} {"age":"" "name":"John Giannandrea" "since":2018 "title":"Senior Vice President - Machine Learning and AI Strategy"} {"age":"" "name":"Deirdre O'Brien" "since":2019 "title":"Senior V ice President - Retail + People"} {"age":55 "name":"Daniel J. Riccio" "since":2012 "title":"Senior Vice President - Hardware Engineering"} {"age":53 "name":"Johny Srouji" "since":2015 "title":"Senior Vice President - Hardware Technologies"} {"age":"" "name":"Jonathan P. Ive" "since":"" "title":"Chief Design Officer"} {"age":70 "name":"James A. Bell" "since":2015 "title":"Independent Director"} {"age":70 "name":"Albert A. Gore" "since":2003 "title":"Independent Director"} {"age":59 "name":"Andrea Jung" "since":2008 "title":"Independent Director"} {"age":70 "name":"Ronald D. Sugar" "since":2010 "title":"Independent Director"} {"age":57 "name":"Sue Wagner" "since":2014 "title":"Independent Director"}] "symbol":"AAPL"}

How do I get it so I can define the header row values and make sure the values alligned to each header as they should (ie: age values all under the age header, name values all under the name header etc)

This is what I would like it to be like :

What I would like it to be

Any help is greatly appreciated!

You should use the DictWriter of csv module for this purpose. here is the example from python documentation DictWriter

import requests
import json
import csv
from pprint import pprint

r = requests.get('https://finnhub.io/api/v1/stock/executive?symbol=AAPL&token=APIKEYHERE')

with open('output.csv', 'w') as csvfile:
    fieldnames = ['name', 'age', 'title', 'since']
    writer = csv.DictWriter(csvfile, fieldnames=fieldnames)
    writer.writeheader()
    recs = r.json().get('executive')
    for rec in recs:
        writer.writerow(rec)
exit()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM