Last active
March 28, 2018 11:58
-
-
Save SergioLaRosa/f87ad6f2f32292a6cf03749c00404bfe to your computer and use it in GitHub Desktop.
A fast, simple script for writing Python dicts to CSV
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #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