Created
February 6, 2018 21:52
-
-
Save dougn/cd014afa228f97ba0766b7ab3f928ae8 to your computer and use it in GitHub Desktop.
convert a bunch of json files into a csv file
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
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() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Now csv writers are context managers you can write