Last active
August 29, 2015 13:57
-
-
Save kevbradwick/9413873 to your computer and use it in GitHub Desktop.
quick csv2json example
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
#!/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