Skip to content

Instantly share code, notes, and snippets.

@kevbradwick
Last active August 29, 2015 13: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 kevbradwick/9413873 to your computer and use it in GitHub Desktop.
Save kevbradwick/9413873 to your computer and use it in GitHub Desktop.
quick csv2json example
#!/usr/bin/env python
"""
Usage: cat mycsvfile.csv | ./csv2json.py > myjson.json
"""
import fileinput
import json
rows = []
headers = []
first_row = True
for line in fileinput.input():
if first_row:
headers = [h.strip() for h in line.split(',')]
first_row = False
else:
cols = [c.strip() for c in line.split(',')]
seq = [(headers[i], cols[i]) for i in range(0, len(headers))]
rows.append({key:val for (key, val) in seq})
print json.dumps(rows, indent=2)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment