如何在python中创建下面的嵌套字典?

I have data stored in csv file as data .Now I want to create a nested dictionary in python maintaining same hierarchy like this.

{A:{B:[C:{E:{}},D:{}]

我无法创建这个。 下面是我的代码:

*import pandas as pd
li=[]
def s(x):
    l=df["Source"][df["Reference"]==str(x)]
    print("printing l",len(l))
    l1=str(l).split()
    print("printing l1",l1)
    return l1[1]
df=pd.read_csv("file.csv")
new_dict = current = {}
name="Root"
while name in df['Reference'].values:
    i=s(name)
    current[i] = {}
    current = current[i]
    print(new_dict)
    name=i

print(new_dict)*
评论