使用openpyxl使用电子表格的行创build对象列表

我在Excel中有一个表格,如下所示:

ID Address Type 1 2 John Avenue family 2 3 John Avenue single woman 3 4 John Avenue single man 4 5 John Avenue mixed couple 5 6 John Avenue youth 6 7 John Avenue family 7 8 John Avenue single woman 

我想读取表中的每一行(不包括标题),并将每行转换为“Person”类的实例。

为了做到这一点,我试图把每一行读成一个字典,以“ID”作为关键。 我正在使用“openpyxl”从Excel文件中读取。 一旦我能够将Excel文件中的数据存储到字典中,我计划创build“Person”类的实例 – 尽pipe我不确定这是否是最好的方法。

这是我到目前为止的代码:

 from openpyxl import load_workbook class Person(object): def __init__(self, id, address, type): self.id = id self.address = address self.type = type wb = load_workbook('sample.xlsx') sheet = wb.worksheets[0] row_count = sheet.max_row column_count = sheet.max_column header = [] for col in range (column_count): header.append(sheet.cell(0, col)) data = [] for row in range(1, row_count): dict = {} for col in range(column_count): dict[header[row]]=sheet.cell(row,col) data.append(dict) print (data) 

我收到以下错误信息:

AttributeError:'int'对象没有属性'upper'

我认为这可能是一个索引错误,但我试图解决它没有运气。

预先感谢您的帮助!

我认为以下应该做你想要的。

 from openpyxl import load_workbook class Person(object): def __init__(self, id=None, address=None, type=None): self.id = id # avoid using Python keywords where possible self.address = address self.type = type wb = load_workbook('sample.xlsx') sheetname = "Addresses" ws = wb[sheetname] # openpyxl >= 2.4 header = [cell.value for cell in ws[1]] # openpyxl < 2.4 header = [cell.value for cell in ws.rows[0]] peopele = [] for row in ws.rows[1:]: values = {} for key, cell in zip(header, row): values[key] = cell.value person = Person(**values) people.append(person) # alternatively if the order is fixed for row in ws.rows[1:]: args = [cell.value for cell in row] person = Person(*args) people.append(person) 

请注意,最好不要覆盖Python关键字或将其用作属性来减less混淆。