Skip to content

Instantly share code, notes, and snippets.

@dougn
Created February 6, 2018 21:52
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 dougn/cd014afa228f97ba0766b7ab3f928ae8 to your computer and use it in GitHub Desktop.
Save dougn/cd014afa228f97ba0766b7ab3f928ae8 to your computer and use it in GitHub Desktop.
convert a bunch of json files into a csv file
import argparse
import csv
import json
parser = argparse.ArgumentParser(
description="""
Convert a number of json files into a single CSV file with each file as a row.""",
epilog="""
Example:
""")
parser.add_argument('csvfile', type=argparse.FileType('w'),
help="csv file to generate")
parser.add_argument('jsonfile', nargs='+', type=argparse.FileType('r'),
help="json files to parse")
def main():
args = parser.parse_args()
data = [json.load(jf) for jf in args.jsonfile]
keys = sorted(set(k for d in data for k in d ))
out = csv.DictWriter(args.csvfile, keys, extrasaction='ignore')
out.writeheader()
for entry in data:
out.writerow(entry)
if __name__ == '__main__':
main()
@holdenweb
Copy link

Now csv writers are context managers you can write

with csv.DictWriter(args.csvfile, keys, extrasaction='ignore') as out:
    out.writeheader()
    for entry in data:
        out.writerow(entry)

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