Skip to content

Instantly share code, notes, and snippets.

@nwandabridges
nwandabridges / dictionary_to_excel_formatted_csv.py
Last active May 30, 2022 03:19
Create a CSV that is properly formatted for MS Excel
import csv
arbitrary_list_of_dictionaries = [{'hello': 'world'}, {'hello': 'you'}]
arbitrary_output_path = 'output.csv'
# Use the keys from the first dictionary as field names in CSV
field_names = [key for key in arbitrary_list_of_dictionaries[0].keys()]
# Note: The 'encoding' argument is important. It adds a BOM (byte order mark)
# to the beginning of the file.