import csv import json data = [] input_file = csv.DictReader(open("huntington - Sheet1.csv")) for row in input_file: ##print row data.append(row) ##raise SystemExit # colors from https://www.colorhexa.com/color-names # now we have the data, so we define the legend legend_trials = { "trial_1" : { "id" : 1, "name" : "HDClarity", "img" : "", "color": "5d8aa8", "link" : "https://hdtrialfinder.net/hdclarity-study/" }, "trial_2" : { "id" : 2, "name" : "GENERATION HD1", "img" : "", "color": "e32636", "link" : "https://hdtrialfinder.net/generation-hd1/" }, "trial_3" : { "id" : 3, "name" : "ENROLL-HD", "img" : "", "color": "efdecd", "link" : "https://hdtrialfinder.net/enroll-hd-study/" }, "trial_4" : { "id" : 4, "name" : "HD-DBS", "img" : "", "color": "ffbf00", "link" : "https://hdtrialfinder.net/deep-brain-stimulation-dbs/" }, "trial_5" : { "id" : 5, "name" : "DOMINO-HD", "img" : "", "color": "9966cc", "link" : "https://hdtrialfinder.net/deep-brain-stimulation-dbs/" }, "trial_6" : { "id" : 6, "name" : "HEALTHE-RND", "img" : "", "color": "a4c639", "link" : "https://hdtrialfinder.net/deep-brain-stimulation-dbs/" }, "trial_7" : { "id" : 7, "name" : "PACE-HD", "img" : "", "color": "cd9575", "link" : "https://hdtrialfinder.net/deep-brain-stimulation-dbs/" }, "trial_8" : { "id" : 8, "name" : "PRECISION-HD1", "img" : "", "color": "915c83", "link" : "https://hdtrialfinder.net/deep-brain-stimulation-dbs/" }, "trial_9" : { "id" : 9, "name" : "PRECISION-HD2", "img" : "", "color": "faebd7", "link" : "https://hdtrialfinder.net/deep-brain-stimulation-dbs/" }, "trial_10" : { "id" : 10, "name" : "LCR-MH", "img" : "", "color": "008000", "link" : "https://hdtrialfinder.net/deep-brain-stimulation-dbs/" }, "trial_11" : { "id" : 11, "name" : "GEN-PEAK", "img" : "", "color": "fbceb1", "link" : "https://hdtrialfinder.net/deep-brain-stimulation-dbs/" }, "trial_12" : { "id" : 12, "name" : "EIP19-NFD-401", "img" : "", "color": "00ffff", "link" : "https://hdtrialfinder.net/deep-brain-stimulation-dbs/" }, "trial_13" : { "id" : 13, "name" : "C-000418-2", "img" : "", "color": "7fffd4", "link" : "https://hdtrialfinder.net/deep-brain-stimulation-dbs/" }, "trial_14" : { "id" : 14, "name" : "Natural History", "img" : "", "color": "4b5320", "link" : "https://hdtrialfinder.net/deep-brain-stimulation-dbs/" }, "trial_15" : { "id" : 15, "name" : "Win-HD", "img" : "", "color": "cc6633", "link" : "https://hdtrialfinder.net/deep-brain-stimulation-dbs/" }, "trial_16" : { "id" : 16, "name" : "DIACCIMEX", "img" : "", "color": "e9d66b", "link" : "https://hdtrialfinder.net/deep-brain-stimulation-dbs/" }, "trial_17" : { "id" : 17, "name" : "CrEAM-HD", "img" : "", "color": "87a96b", "link" : "https://hdtrialfinder.net/deep-brain-stimulation-dbs/" }, "trial_18" : { "id" : 18, "name" : "TRIHEP3", "img" : "", "color": "6e7f80", "link" : "https://hdtrialfinder.net/deep-brain-stimulation-dbs/" }, "trial_19" : { "id" : 19, "name" : "REVHD", "img" : "", "color": "007fff", "link" : "https://hdtrialfinder.net/deep-brain-stimulation-dbs/" }, "trial_20" : { "id" : 20, "name" : "BIOHD", "img" : "", "color": "89cff0", "link" : "https://hdtrialfinder.net/deep-brain-stimulation-dbs/" }, "trial_21" : { "id" : 21, "name" : "HD123", "img" : "", "color": "a1caf1", "link" : "https://hdtrialfinder.net/deep-brain-stimulation-dbs/" } } ## Extra colours : cccc00, ff9900, ffff33 ##ff = 1 ##print(legend_trials["trial_"+str(ff)]["name"] ) data1 = [] for datapoint in data: #print( datapoint ) trialnames = [ legend_trials["trial_"+str(i.strip())]["name"] for i in datapoint["trial_list"].strip().split(",") ] datapoint["trial_list_names"] = trialnames triallinks = [ legend_trials["trial_"+str(i.strip())]["link"] for i in datapoint["trial_list"].strip().split(",") ] datapoint["trial_list_links"] = triallinks trialimg = [ legend_trials["trial_"+str(i.strip())]["color"] for i in datapoint["trial_list"].strip().split(",") ] datapoint["marker_color"] = trialimg[0] trialids = [ legend_trials["trial_"+str(i.strip())]["id"] for i in datapoint["trial_list"].strip().split(",") ] datapoint["trial_list_images"] = trialids links_images = "" for id_,link in zip( trialids , triallinks ): links_images += "
 " datapoint["content"] = "

Site Name:
"+datapoint["trial_site_name"]+"
Trial name:
"+( ', '.join( trialnames) )+"

Prinicipal investigator:
"+datapoint["principal_investigator_name"]+"

Site coordinator:
"+datapoint["site_coordinator_name"]+"
"+datapoint["contact_information"]+"

"+datapoint["address"]+"


Click on the icons to read more : "+links_images+"
" data1.append( datapoint ) print( data1 ) string_data = json.dumps(data1) file = open("content.json","w") file.write(string_data) file.close()