Created
May 19, 2012 01:36
-
-
Save thinkjson/2728511 to your computer and use it in GitHub Desktop.
JSON to CSV conversion benchmarking
This file contains hidden or 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
json2csv.py | |
real 5m28.824s | |
user 5m27.584s | |
sys 0m1.524s | |
C (https://github.com/jehiah/json2csv) | |
real 0m15.945s | |
user 0m15.181s | |
sys 0m1.204s |
This file contains hidden or 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 json | |
import csv | |
import sys | |
fields = sys.argv[1].split(",") | |
output = csv.writer(sys.stdout, delimiter=',', quotechar='"') | |
for line in sys.stdin: | |
if line.strip() != "": | |
try: | |
obj = json.loads(line) | |
row = [] | |
for field in fields: | |
if field in obj: | |
row.append(obj[field]) | |
else: | |
row.append(None) | |
output.writerow(row) | |
except ValueError: | |
sys.stderr.write("Error: %s" % line) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment