Oct-28-2020, 02:47 PM
with your code
data = [{'begins_at': '2020-10-22T14:00:00Z', 'open_price': '5.125000', 'close_price': '5.130000', 'high_price': '5.170000', 'low_price': '5.120000', 'volume': 134609, 'session': 'reg', 'interpolated': False, 'symbol': 'PSEC'}, {'begins_at': '2020-10-22T15:00:00Z', 'open_price': '5.135000', 'close_price': '5.140000', 'high_price': '5.150000', 'low_price': '5.130000', 'volume': 48897, 'session': 'reg', 'interpolated': False, 'symbol': 'PSEC'}] import csv with open('twostocks.csv', 'w', newline='') as csvfile: fieldnames = [ 'begins_at', 'open_price', 'close_price', 'high_price', 'low_price', 'volume', 'session', 'interpolated', 'symbol' ] writer = csv.DictWriter(csvfile, fieldnames=fieldnames, extrasaction='ignore') writer.writeheader() writer.writerows(data)this is what i get in the csv file
Output:begins_at,open_price,close_price,high_price,low_price,volume,session,interpolated,symbol
2020-10-22T14:00:00Z,5.125000,5.130000,5.170000,5.120000,134609,reg,False,PSEC
2020-10-22T15:00:00Z,5.135000,5.140000,5.150000,5.130000,48897,reg,False,PSEC
so the problem is indeed with the format of your data. check that it is indeed list of dicts as shown
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs