Created
December 5, 2012 19:28
-
-
Save garbados/4218737 to your computer and use it in GitHub Desktop.
jsonize_csv(): quickly turn CSVs into arrays of dicts
This file contains 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
""" | |
I hate CSVs, so this converts them into an array of dicts, with each key-value pair matching that column's header, and the row's value for that column. | |
""" | |
def jsonize_csv(fp): | |
with open(fp, 'r') as f: | |
headers = f.readline().split(',')[:-1] | |
lines = [] | |
for line in f: | |
line = line.split(',') | |
parsed_line = {} | |
for i, header in enumerate(headers): | |
parsed_line[header] = line[i] | |
lines.append(parsed_line) | |
return lines |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment