从 excel 工作簿制作嵌套字典

Making nested dictionary from an excel workbook

我必须从 excel 工作簿创建嵌套字典。我正在使用 openpyxl 库。 Excel 文件看起来像这样:

|   score    |     lat     |      lon     | loc.country|  loc.city   |  loc.street  |   loc.st_nr  | ...
| ---------- | ----------- | ------------ | ---------- | ----------- | ------------ | ------------ | ...
|     2      |     51      |      19      |  Poland    |   Warsaw    |    Cicha     |      1       | ...
|     4      |     52      |      18      |  Poland    |   Cracow    |    Dluga     |      2       | ...
|    ...     |     ...     |     ...      |    ...     |     ...     |     ...      |     ...      | ...

我想实现这样的目标:

dict = {
    "score": 2,
    "lat": 51,
    "lon": 19,
    "loc": {
        "country": "Poland",
        "city": "Warsaw",
        "street": "Cicha",
        "st_nr": 1
    }
}

到目前为止我所做的是从 header 中获取键列表,从行中获取值列表并将它们压缩在一起:

...
wb = load_workbook(file_obj)
worksheet = wb.active
rows = worksheet.iter_rows(values_only=True)
header = next(rows)
for row in rows:
    values = row
    order = dict(zip(header, row))
...

但它不会生成嵌套字典。我实现的是:

    dict = {
        "score": 2,
        "lat": 51,
        "lon": 19,
        "loc.country": "Poland",
        "loc.city": "Warsaw",
        "loc.street": "Cicha",
        "loc.st_nr": 1
    }

如何修改它以获得预期的结果?

不是特别好的选择,但也懒得安装 xD

输入:

mydict = {
    "score": 2,
    "lat": 51,
    "lon": 19,
    "loc.country": "Poland",
    "loc.city": "Warsaw",
    "loc.street": "Cicha",
    "loc.st_nr": 1
}

函数

mydict = {}
for key in dict.keys():
    key = key.split('.')
    if key.__len__() == 2:
        if key[0] not in mydict.keys():
            mydict[key[0]] = {}
        if key[1] not in mydict.keys():
            mydict[key[0]][key[1]] = dict[key[0] + "." + key[1]]
    else:
        mydict[key[0]] = dict[key[0]]

print(mydict)

输出:

{'score': 2, 'lat': 51, 'lon': 19, 'loc': {'country': 'Poland', 'city': 'Warsaw', 'street': 'Cicha', 'st_nr': 1}}

你不能只用 dict() 来做,因为这个函数只是用你的键和值创建一个平面字典。如果你想降低变量名中带点的级别,则必须使用自定义函数。

如果您传递一个变量名列表和一个值列表,下面的函数将嵌套所有带有点的变量。

def nest_dict(keys,values):
    d = {}
    for i in range(len(keys)):
        if '.' in keys[i]:
            l1,l2 = keys[i].split('.')[0],''.join(keys[i].split('.')[1:])
            try:
                d[l1][l2]=values[i]
            except:
                d[l1]={l2:values[i]}
        else:
            d[keys[i]]=values[i]
    return d

对于您的数据,您将传递 header 和这样的行:

header = ["score","lat","lon","loc.country","loc.city","loc.street","loc.st_nr"]
row = [2,51,19,"Poland","Warsaw","Cicha",1]

print(nest_dict(header,row))

哪个returns字典

{'score': 2, 
 'lat': 51, 
 'lon': 19, 
 'loc': {'country': 'Poland', 
         'city': 'Warsaw', 
         'street': 'Cicha', 
         'st_nr': 1}
}

请注意,这仅适用于一个级别。如果你的变量名有多个点,必须更深一层,你就必须调整函数。