我有一个电子表格,我试图将数据导入一组嵌套的python词典。本质上,电子表格包含用于站点,建筑物,楼层,房间,行和机架的列。我希望数据结构如下所示:
sites = [
{
"name": "",
"descr": "",
"buildings": [
{
"name": "",
"descr": "",
"floors": [
{
"name": "",
"descr": "",
"rooms": [
{
"name": "",
"descr": "",
"rows": [
{
"name": "",
"descr": "",
"racks": [
{
"name": "",
"descr": ""
}
]
}
]
}
]
}
]
}
]
}
]
电子表格的一个例子是:
+------+---------------+----------+----------------+-------+-------------+------+------------+-----+-----------+------+------------+-------------------------------------------------------------------+
| site | site_descr | building | building_descr | floor | floor_descr | room | room_descr | row | row_descr | rack | rack_descr | rack_dn |
+------+---------------+----------+----------------+-------+-------------+------+------------+-----+-----------+------+------------+-------------------------------------------------------------------+
| dc1 | Data Center 1 | alpha | Alpha Building | 1 | Floor 1 | 100 | Room 100 | A | Row A | A5 | Rack A5 | uni/fabric/site-dc1/building-alpha/floor-1/room-100/row-A/rack-A5 |
+------+---------------+----------+----------------+-------+-------------+------+------------+-----+-----------+------+------------+-------------------------------------------------------------------+
| dc1 | Data Center 1 | alpha | Alpha Building | 1 | Floor 1 | 100 | Room 100 | A | Row A | A5 | Rack A5 | uni/fabric/site-dc1/building-alpha/floor-1/room-100/row-A/rack-A5 |
+------+---------------+----------+----------------+-------+-------------+------+------------+-----+-----------+------+------------+-------------------------------------------------------------------+
| dc1 | Data Center 1 | alpha | Alpha Building | 1 | Floor 1 | 200 | Room 200 | A | Row A | A5 | Rack A5 | uni/fabric/site-dc1/building-alpha/floor-1/room-200/row-A/rack-A5 |
+------+---------------+----------+----------------+-------+-------------+------+------------+-----+-----------+------+------------+-------------------------------------------------------------------+
| dc1 | Data Center 1 | alpha | Alpha Building | 1 | Floor 1 | 100 | Room 100 | B | Row B | B5 | Rack B5 | uni/fabric/site-dc1/building-alpha/floor-1/room-100/row-B/rack-B5 |
+------+---------------+----------+----------------+-------+-------------+------+------------+-----+-----------+------+------------+-------------------------------------------------------------------+
| dc1 | Data Center 1 | alpha | Alpha Building | 2 | Floor 2 | 100 | Room 100 | A | Row A | A7 | Rack A7 | uni/fabric/site-dc1/building-alpha/floor-2/room-100/row-A/rack-A7 |
+------+---------------+----------+----------------+-------+-------------+------+------------+-----+-----------+------+------------+-------------------------------------------------------------------+
| dc2 | Data Center 2 | beta | Beta Building | 5 | Floor 5 | 200 | Room 200 | B | Row B | B5 | Rack B5 | uni/fabric/site-dc2/building-beta/floor-5/room-200/row-B/rack-B5 |
+------+---------------+----------+----------------+-------+-------------+------+------------+-----+-----------+------+------------+-------------------------------------------------------------------+
将这个纳入我的数据结构的最佳方法是什么? pyexcel模块可以导入记录,这些记录基本上创建了一个字典列表,每行都作为列表中的条目。我在修改这个逻辑时遇到了问题...
我应该在for循环之前初始化结构吗?我应该在填充结构时构建结构吗?如果我用下面的空格初始化结构,我需要确保我的第一行填充那些空白,这让我认为后者可能是更好的选择。
答案 0 :(得分:0)
我认为要做的是遍历列名,查找具有正确名称的字典,如果它不存在则创建它,然后进入其子类数组:
import pprint
columns = ['site', 'building', 'floor', 'room', 'row', 'rack']
keys = ['buildings', 'floors', 'rooms', 'rows', 'racks']
def find(seq, pred):
try:
found = next(x for x in seq if pred(x))
except StopIteration:
found = None
return found
def add_record(sites, record):
array = sites
for index, column in enumerate(columns):
name = record[column]
descr = record[column + '_descr']
dictionary = find(array, lambda x: x['name'] == name)
if dictionary is None:
dictionary = {'name': name, 'descr' : descr}
if column != 'rack':
dictionary[keys[index]] = []
array.append(dictionary)
if column != 'rack':
array = dictionary[keys[index]]
else:
dictionary['rack_dn'] = record['rack_dn']
def main():
records = [{'site': 'dc1', 'site_descr' : 'Data Center 1', 'building' : 'alpha',
'building_descr': 'Alpha Building', 'floor' : 1, 'floor_descr' : 'Floor 1',
'room' : 100, 'room_descr' : 'Room 100', 'row' : 'A', 'row_descr': 'Row A',
'rack': 'A5', 'rack_descr' : 'Rack A5',
'rack_dn' : 'uni/fabric/site-dc1/building-alpha/floor-1/room-100/row-A/rack-A5'},
{'site': 'dc1', 'site_descr' : 'Data Center 1', 'building' : 'alpha',
'building_descr': 'Alpha Building', 'floor' : 1, 'floor_descr' : 'Floor 1',
'room' : 200, 'room_descr' : 'Room 200', 'row' : 'A', 'row_descr': 'Row A',
'rack': 'A5', 'rack_descr' : 'Rack A5',
'rack_dn' : 'uni/fabric/site-dc1/building-alpha/floor-1/room-200/row-A/rack-A5'},
{'site': 'dc2', 'site_descr' : 'Data Center 2', 'building' : 'beta',
'building_descr': 'Beta Building', 'floor' : 5, 'floor_descr' : 'Floor 5',
'room' : 200, 'room_descr' : 'Room 200', 'row' : 'B', 'row_descr': 'Row B',
'rack': 'B5', 'rack_descr' : 'Rack B5',
'rack_dn' : 'uni/fabric/site-dc2/building-beta/floor-5/room-200/row-B/rack-B5'}]
sites = []
for record in records:
add_record(sites, record)
pp = pprint.PrettyPrinter()
pp.pprint(sites)