Skip to content

Instantly share code, notes, and snippets.

@Saigesp
Last active December 11, 2019 21:55
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 Saigesp/09e774f5612e00cd5637b83dd234dfa2 to your computer and use it in GitHub Desktop.
Save Saigesp/09e774f5612e00cd5637b83dd234dfa2 to your computer and use it in GitHub Desktop.
Convert a 3 columns csv to a hierarchical json with d3 sunburst default format
import csv
import json
import itertools
"""
Convert a 3 columns CSV to a hierarchical JSON
with d3 sunburst default format.
Duplicated CSV column values are nested on the JSON as follows:
input file (CSV):
lorem,ipsum,dolor
lorem,ipsum,amet
output file (JSON):
{
"name": "Output",
"children": [{
"name": "lorem",
"children": [{
"name": "ipsum",
"children": [{
"name": "dolor",
"value": 1
},{
"name": "amet",
"value": 1
}]
}]
}]
}
"""
INPUTFILE="input.csv"
OUTPUTFILE="output.json"
output = {
'name': 'output',
'children': []
}
with open(INPUTFILE, 'r') as file:
reader = csv.reader(file)
count = 0
for row in reader:
count += 1
if not row[0] in [x['name'] for x in output['children']]:
output['children'].append({'name': row[0], 'children':[]})
if not row[1] in list(y['name'] for y in list(itertools.chain.from_iterable([x['children'] for x in output['children']]))):
index = next((i for (i, d) in enumerate(output['children']) if d["name"] == row[0]), None)
output['children'][index]['children'].append({'name': row[1], 'children':[]})
if not row[2] in list(z['name'] for z in list(itertools.chain.from_iterable((y['children'] for y in list(itertools.chain.from_iterable([x['children'] for x in output['children']])))))):
index1 = next((i for (i, d) in enumerate(output['children']) if d["name"] == row[0]), None)
index2 = next((i for (i, d) in enumerate(output['children'][index1]['children']) if d["name"] == row[1]), None)
output['children'][index1]['children'][index2]['children'].append({'name': row[2], 'value':1})
with open(OUTPUTFILE, 'w') as file:
json.dump(output, file, ensure_ascii=False)
print('done')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment