Last active
May 12, 2019 06:33
-
-
Save ihgs/caf192943889607afe3c4175fd776917 to your computer and use it in GitHub Desktop.
json listをNewline Delimited JSONに変換する
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 csv | |
from io import StringIO | |
import json | |
if __name__ == '__main__': | |
""" | |
sample1) cat data_with_header.csv | python csv2json.py > test.json | |
""" | |
csv_str = open(0).read() | |
data = csv.DictReader(StringIO(csv_str)) | |
print(json.dumps([ d for d in data], indent=2)) |
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 json | |
import sys | |
import argparse | |
if __name__ == '__main__': | |
""" | |
sample1) cat test.json | python json2ndjson.py > test.ndjson | |
sample2) cat test.json | python json2ndjson.py --key nodes > nodes.ndjson | |
""" | |
json_str = open(0).read() | |
json_obj = json.loads(json_str) | |
parser = argparse.ArgumentParser() | |
parser.add_argument("--key") | |
args = parser.parse_args() | |
if (args.key): | |
json_obj = json_obj[args.key] | |
if (isinstance(json_obj, list)): | |
for j in json_obj: | |
print(json.dumps(j)) | |
else: | |
sys.stderr.write('Not list json') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment