I have the following csv
id, o1, o2, o3
'jess', 1.0, 4, 0.3
'jill', 0, 5, 0.123
'jamie', -3, 0.2, 1.0
and would like it in a nested json with each column as a json keyed on the header name:
myjson = {
"o1": {"jess": 1.0, "jill": 0, "jamie": -3},
"o2": {"jess": 4, "jill": 5, "jamie": 0.2},
"o3": {"jess": 0.3, "jill": 0.2, "jamie": 1.0},
Not sure the best (most pythonic) way to do this. Here is my first attempt:
import csv
with open(myfile, "r") as f:
reader = csv.reader(f, delimiter=',', quotechar='"')
first = True
for line in reader:
if first:
myjson = {key: dict() for key in line}
header = list(line)
first = False
for i in range(len(header)):
id = line[0]
myjson[header[i+1]][id] = line[i+1]
I assume there is a better way to do this.
Edit: Should've specified this earlier but I do NOT want to use anything like Pandas. This needs to be super fast with minimal package dependencies.