summaryrefslogtreecommitdiffstats
path: root/json2csv.py
diff options
context:
space:
mode:
authorAaron LI <aaronly.me@outlook.com>2016-05-06 20:02:40 +0800
committerAaron LI <aaronly.me@outlook.com>2016-05-06 20:02:40 +0800
commitb89564d7419df4c81416802496629e63ffb54b66 (patch)
tree683256cf4802131259246f1f4658c07ecc9d8db0 /json2csv.py
parent90ce99e03ca7ca43041815096d1284b423dfef7e (diff)
downloadcexcess-b89564d7419df4c81416802496629e63ffb54b66.tar.bz2
json2csv.py: extract JSON results and output as CSV format
Diffstat (limited to 'json2csv.py')
-rwxr-xr-xjson2csv.py39
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()