kaushik guru

kaushik guru

  • NA
  • 252
  • 30.4k

how to save api output json as csv file

Sep 21 2020 11:24 PM
I have an API output as follows
 
{ "sub_start_date_end_date": [
{ "account_id": "10996fd6-a708-4d70-b65e-50620d8fbbdf",
"country": "'USA'",
"end_date": "Fri, 03 Sep 2021 00:00:00 GMT",
"start_date": "Thu, 03 Sep 2020 00:00:00 GMT" } ] }
 
I need to save it as create and save it as a csv file
 
@final.route('/start_date-end_datess', methods=['GET'])
def subscription_datess():
subscription_id = request.args.get('subscription_id')
email = request.args.get('email')
update_query = '''
some query '''
result = db.session.execute(text(update_query), {'a':email})
final = [dict(i) for i in result]
excel = json.load(final)
files = csv.writer(open("test.csv", "wb+"))
files.writerow(["account_id", "country", "end_date", "start_date"])
for excel in excel:
files.writerow([excel["account_id"],
excel["country"],
excel["end_date"],
excel["start_date"]])
return{"sub_start_date_end_date":final}
 
when executing this end point I am getting the following error list' object has no attribute 'read'
 
Kindly guide

Answers (3)