diff options
author | Aaron LI <aaronly.me@outlook.com> | 2016-05-06 20:02:40 +0800 |
---|---|---|
committer | Aaron LI <aaronly.me@outlook.com> | 2016-05-06 20:02:40 +0800 |
commit | b89564d7419df4c81416802496629e63ffb54b66 (patch) | |
tree | 683256cf4802131259246f1f4658c07ecc9d8db0 /json2csv.py | |
parent | 90ce99e03ca7ca43041815096d1284b423dfef7e (diff) | |
download | cexcess-b89564d7419df4c81416802496629e63ffb54b66.tar.bz2 |
json2csv.py: extract JSON results and output as CSV format
Diffstat (limited to 'json2csv.py')
-rwxr-xr-x | json2csv.py | 39 |
1 files changed, 39 insertions, 0 deletions
diff --git a/json2csv.py b/json2csv.py new file mode 100755 index 0000000..a9bcc81 --- /dev/null +++ b/json2csv.py @@ -0,0 +1,39 @@ +#!/usr/bin/env python3 +# -*- coding: utf-8 -*- +# +# Read results in JSON format and output as CSV format. +# +# Aaron LI +# Created: 2016-04-27 +# Updated: 2016-05-06 +# + +import sys +import json +import csv +import argparse +from collections import OrderedDict + + +def main(): + parser = argparse.ArgumentParser( + description="Extract excess results from excess.json") + parser.add_argument("json", help="input JSON file") + parser.add_argument("csv", nargs="?", help="optional output CSV file") + args = parser.parse_args() + + results = json.load(open(args.json), object_pairs_hook=OrderedDict) + + csv_writer = csv.writer(sys.stdout) + csv_writer.writerow(results.keys()) + csv_writer.writerow(results.values()) + + if args.csv: + with open(args.csv, "w") as csv_file: + csv_writer = csv.writer(csv_file) + csv_writer.writerow(results.keys()) + csv_writer.writerow(results.values()) + + +if __name__ == "__main__": + main() |