Notes

Source: 📖 Python Cookbook ch6.1 p177


Writing CSV data

Writing is similar to Reading CSV data.



import csv


headers = ['Symbol', 'Price', 'Date', 'Time', 'Change', 'Volume']
rows =  [('AA', 39.48, '6/11/2007', '9:36am', -0.18, 181800),
('AIG', 71.38, '6/11/2007', '9:36am', -0.15, 195500),
('AXP', 62.58, '6/11/2007', '9:36am', -0.46, 935000),]

with open('stocks.csv', 'w'), as f:
	f_csv = csv.writer(f)
	f_csv.writerow(headers)
	f_csv.writerow(rows)

csv.DictWriter can also be used for working with data stored as a python dictionary.



rows = [{'Symbol':'AA', 'Price':39.48, 'Date':'6/11/2007', 'Time':'9:36am', 'Change':-0.18, 'Volume':181800}, 
'Symbol':'AIG', 'Price': 71.38, 'Date':'6/11/2007', 'Time':'9:36am', 'Change':-0.15, 'Volume': 195500},
{'Symbol':'AXP', 'Price': 62.58, 'Date':'6/11/2007', 'Time':'9:36am', 'Change':-0.46, 'Volume': 935000},]

with open('stocks.csv', 'w') as f:
	f_csv = csv.DictWriter(f, headers)
	f_csv.writeheader()
	f_csv.writerows(rows)