Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
csv Dictwriter problems
#5
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

Reply


Messages In This Thread
csv Dictwriter problems - by fsuedr2022 - Oct-28-2020, 02:15 PM
RE: csv Dictwriter problems - by buran - Oct-28-2020, 02:20 PM
RE: csv Dictwriter problems - by fsuedr2022 - Oct-28-2020, 02:24 PM
RE: csv Dictwriter problems - by buran - Oct-28-2020, 02:45 PM
RE: csv Dictwriter problems - by buran - Oct-28-2020, 02:47 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Iterating a dictwriter pstarrett 0 2,666 Feb-19-2018, 01:44 AM
Last Post: pstarrett

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020