Skip to content

Instantly share code, notes, and snippets.

@SergioLaRosa
Last active March 28, 2018 11:58
Show Gist options
  • Save SergioLaRosa/f87ad6f2f32292a6cf03749c00404bfe to your computer and use it in GitHub Desktop.
Save SergioLaRosa/f87ad6f2f32292a6cf03749c00404bfe to your computer and use it in GitHub Desktop.
A fast, simple script for writing Python dicts to CSV
#Written by Sergio La Rosa ([email protected])
#https://gist.github.com/SergioLaRosa/
#IMPORTANT: Python 3 only
#Standard dict format
'''
'key':'value'
'''
#Standard CSV format
'''
'key','value'
'''
import os
import sys
import csv
def fast_writer(f_name, text_dict):
try:
# Windows
if os.name == "nt":
with open(f_name, 'w', newline='') as csv_file:
writer = csv.writer(csv_file)
for key, value in text_dict.items():
writer.writerow([key, value])
# Unix/Linux
else:
with open(f_name, 'w') as csv_file:
writer = csv.writer(csv_file)
for key, value in text_dict.items():
writer.writerow([key, value])
except Exception as err:
print(err)
sys.exit()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment