Skip to content Skip to sidebar Skip to footer

How To Convert Csv To Hierarchial Json

I have a csv file which is of the form: Category Sub-Category Value A A1 1 A A2 2 A A3 5 B B1 10 B

Solution 1:

This would work

import json
f = open('path/to/file','r')
arr=[]
headers = []

for header in f.readline().split(','):
headers.append(header)



for line in f.readlines():
lineItems = {}

for i,item in enumerate(line.split(',')):  
lineItems[headers[i]] = item


arr.append(lineItems)

f.close()

jsonText = json.dumps(arr)

print jsonText

The source.


Post a Comment for "How To Convert Csv To Hierarchial Json"