Skip to content

Instantly share code, notes, and snippets.

@sshh12
Last active July 7, 2020 23:55
Show Gist options
  • Save sshh12/9b81975505a48855991949d6a57a9d19 to your computer and use it in GitHub Desktop.
Save sshh12/9b81975505a48855991949d6a57a9d19 to your computer and use it in GitHub Desktop.
import json
import sys
import csv
def main(fn):
with open(fn, 'r', newline='') as csvfile:
reader = csv.DictReader(csvfile)
rows = [dict(row) for row in reader]
with open('data.json', 'w') as jsonfile:
jsonfile.write(json.dumps(rows, indent=2))
if __name__ == "__main__":
args = sys.argv
if len(args) == 2:
fn = args[1]
else:
fn = input('csv filename > ')
main(fn)
@sshh12
Copy link
Author

sshh12 commented Jul 7, 2020

python3 csv-to-json.py my.csv

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment