Skip to content

Instantly share code, notes, and snippets.

@bdunnette
Created April 6, 2018 14:51
Show Gist options
  • Save bdunnette/7e47efc3cacffca559a217d27b9ab95e to your computer and use it in GitHub Desktop.
Save bdunnette/7e47efc3cacffca559a217d27b9ab95e to your computer and use it in GitHub Desktop.
import sys
import os
import json
from csv import DictReader
files_to_parse = sys.argv[1:]
for f in files_to_parse:
print("Reading cases from %s" % f)
json_filename = "%s.json" % os.path.splitext(f)[0]
cases = []
with open(f) as csvfile:
reader = DictReader(csvfile)
for row in reader:
case_dict = dict(row)
description = case_dict['Comment/History'].replace("\n", " ").strip()
patient = dict()
patient['age'] = int(case_dict['Age'])
patient['sex'] = case_dict['Gender'].upper()
slide_list = [case_dict['Image ID']]
case = {'slides': slide_list, 'description': description, 'patient': patient}
print(" Found case: %s" % case)
cases.append(case)
cases_json = json.dumps({'cases': cases}, indent=2)
with open(json_filename, "w") as json_file:
print("Writing JSON to %s" % json_filename)
json_file.write(cases_json)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment