Skip to content

Instantly share code, notes, and snippets.

@akszydelko
Last active May 23, 2016 11:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save akszydelko/ab370605206f23d6d3d0f1024ce2d0e2 to your computer and use it in GitHub Desktop.
Save akszydelko/ab370605206f23d6d3d0f1024ce2d0e2 to your computer and use it in GitHub Desktop.
Export csv that opens in Excl without formatting problems
import codecs
import csv
with open('data.csv', 'wb') as f:
# Write BOM to the file, it is required
# Excl without BOM does not understend UTF-8 charcters... (Non ascii characters)
f.write(codecs.BOM_UTF8)
# Set excl dialect
# Delimiter ; is required so columns are detected properly
writer = csv.writer(f, dialect=csv.excel, delimiter=';') # Option 1
dict_writer = csv.DictWriter(f, dialect=csv.excel, delimiter=';', fieldnames=['a', 'b', 'c']) # Option 2
# Continiue normally
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment