Skip to content

Instantly share code, notes, and snippets.

@ihgs
Last active May 12, 2019 06:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ihgs/caf192943889607afe3c4175fd776917 to your computer and use it in GitHub Desktop.
Save ihgs/caf192943889607afe3c4175fd776917 to your computer and use it in GitHub Desktop.
json listをNewline Delimited JSONに変換する
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))
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