Skip to content

Instantly share code, notes, and snippets.

@garbados
Created December 5, 2012 19:28
Show Gist options
  • Save garbados/4218737 to your computer and use it in GitHub Desktop.
Save garbados/4218737 to your computer and use it in GitHub Desktop.
jsonize_csv(): quickly turn CSVs into arrays of dicts
"""
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