我在工作簿中有多个excels工作表,名称为sheet1、sheet2、sheet3,我正在使用标题作为键将每个工作表中的数据转换为字典。但现在我想创建一个嵌套字典,在其中我添加表名作为上述每个字典的键。我的桌子有多张这样的表格:
表一
IP Address prof type
xx.xx.xx abc xyz
xx.xx.xx efg ijk
表2
from xlrd import open_workbook
book = open_workbook('workbook.xls')
sheet = book.sheet_by_index(0)
sheet_names = book.sheet_names()
# reading header values
keys = [sheet.cell(0, col_index).value for col_index in range(sheet.ncols)]
dict_list = []
for row_index in range(1, sheet.nrows):
d = {keys[col_index]: sheet.cell(row_index, col_index).value
for col_index in range(sheet.ncols)}
dict_list.append(d)
print (dict_list)
打印此:
[{'IP Address': 'xx.xx.xx', 'prof': 'abc', 'type': 'xyz'}, {'IP Address':
'xx.xx.xx', 'prof': 'efg', 'type': 'ijk'}]
what I need is :
[{'Sheet1':{'IP Address': 'xx.xx.xx', 'prof': 'abc', 'type': 'xyz'}, {'IP
Address': 'xx.xx.xx', 'prof': 'efg', 'type': 'ijk'}},
{'Sheet2':{'IP Address': 'xx.xx.xx', 'prof': 'abc', 'type': 'xyz'}, {'IP
Address': 'xx.xx.xx', 'prof': 'efg', 'type': 'ijk'}},
]
我在将工作表名称添加为工作簿中多个工作表的键时遇到问题。
任何帮助都将不胜感激。