使用 sheet 名称作为键从 python 中的 excel 工作簿创建字典
Creating dictionary from excel workbook in python using sheet names as key
我在工作簿中有多个 excel sheets,名称如 sheet1、sheet2、sheet3,我正在转换每个 sheet 使用 headers 作为键进入字典。但现在我想创建一个嵌套字典,其中我将 sheet 名称添加为上述每个字典的键。我的 table 有多个 sheet 这种形式:
工作表 1
IP Address prof type
xx.xx.xx abc xyz
xx.xx.xx efg ijk
工作表 2
IP Address prof type
xx.xx.xx abc xyz
xx.xx.xx efg ijk
现在我这样尝试:
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'}},
]
我在将 sheet 名称添加为工作簿中多个 sheet 的键时遇到问题。
我们将不胜感激。
from xlrd import open_workbook
book = open_workbook('Workbook1.xlsx')
pointSheets = book.sheet_names()
full_dict = {}
for i in pointSheets:
# reading header values
sheet = book.sheet_by_name(i)
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)
full_dict[i] = dict_list
dict_list = {}
print (full_dict)
此代码遍历每个 sheet 并附加到“full_dict” sheet_name 后面是您的代码已经为每个 sheet 返回的内容。
名称的获取是参考“How to get excel sheet name in Python using xlrd”
完成的
我在工作簿中有多个 excel sheets,名称如 sheet1、sheet2、sheet3,我正在转换每个 sheet 使用 headers 作为键进入字典。但现在我想创建一个嵌套字典,其中我将 sheet 名称添加为上述每个字典的键。我的 table 有多个 sheet 这种形式: 工作表 1
IP Address prof type
xx.xx.xx abc xyz
xx.xx.xx efg ijk
工作表 2
IP Address prof type
xx.xx.xx abc xyz
xx.xx.xx efg ijk
现在我这样尝试:
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'}},
]
我在将 sheet 名称添加为工作簿中多个 sheet 的键时遇到问题。
我们将不胜感激。
from xlrd import open_workbook
book = open_workbook('Workbook1.xlsx')
pointSheets = book.sheet_names()
full_dict = {}
for i in pointSheets:
# reading header values
sheet = book.sheet_by_name(i)
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)
full_dict[i] = dict_list
dict_list = {}
print (full_dict)
此代码遍历每个 sheet 并附加到“full_dict” sheet_name 后面是您的代码已经为每个 sheet 返回的内容。 名称的获取是参考“How to get excel sheet name in Python using xlrd”
完成的