简体   繁体   English

使用工作表名称作为键从python中的Excel工作簿创建字典

[英]Creating dictionary from excel workbook in python using sheet names as key

I have multiple excels sheets in workbook with names like sheet1, sheet2 , sheet3 and I am converting the data in each sheet into a dictionary using headers as key. 我的工作簿中有多个Excel工作表,它们的名称类似于sheet1,sheet2,sheet3,并且我正在使用标题作为键将每个工作表中的数据转换为字典。 But now I want to create a nested dictionary where i add sheet name as key to each of the above dictionaries. 但是现在我想创建一个嵌套的字典,在其中添加工作表名称作为上述每个字典的键。 My table has multiple sheets of this form: Sheet1 我的表有多个这种形式的工作表:Sheet1

IP Address     prof     type
xx.xx.xx       abc      xyz
xx.xx.xx       efg      ijk

Sheet2 Sheet2中

IP Address     prof     type
xx.xx.xx       abc      xyz
xx.xx.xx       efg      ijk

Now I attempted like this: 现在我尝试这样:

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)

Which prints this : 哪个打印此:

[{'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'}},
       ]

I have problems adding the sheet name as key for multiple sheets in workbook. 我在将工作表名称添加为工作簿中多个工作表的键时遇到问题。

Any help will be appreciated. 任何帮助将不胜感激。

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)

This code iterates over each sheet and appends to ´full_dict´ the sheet_name followed by what your code already returned for each sheet. 此代码遍历每个工作表,并在sheet_name后面附加“ full_dict”,然后是您为每个工作表返回的代码。 The aquisition of names was done referencing " How to get excel sheet name in Python using xlrd " 参考“ 如何使用xlrd在Python中获取Excel工作表名称 ”完成名称的获取

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用Python(和DataNitro)将单元格从一个Excel工作簿中的特定工作表复制到另一个Excel工作簿中的特定工作表 - Using Python (and DataNitro) to copy cells from a particular sheet in one Excel workbook, to a particular sheet in another Excel workbook 如何使用 python 将 excel 工作簿中的内容复制到另一个工作簿而不丢失 excel 格式 - How to copy contents from a sheet of an excel workbook to another workbook without loosing the excel formatting using python 使用python将Excel工作簿表合并为一个 - Combining excel workbook sheet into one using python 如何使用 Python 将每个工作表的列名和 output 提取到新的 excel 工作簿? - How to extract column names of each sheet and output to a new excel workbook using Python? 如何使用 Python 将数据从一个 Excel 工作表复制到同一工作簿的另一工作表? - How to copy data from One Excel sheet to another sheet for same Workbook Using Python? 从Python中创建Excel中的新工作簿 - Creating a new workbook in Excel from Python breaks 用python加密excel工作簿表 - encrypting excel workbook sheet with python 从Excel创建Python字典 - Creating Python Dictionary from excel python从excel创建字典 - python creating dictionary from excel 如何根据当前日期使用 python Pandas 从 excel 工作表加载特定工作簿 - How to load specific workbook from excel sheet using python Pandas based on current date
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM