小编Tha*_*lai的帖子

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

我在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 
Run Code Online (Sandbox Code Playgroud)

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

为了做到这一点,我试图将每一行读入一个字典,以"id"为关键.我正在使用"openpyxl"从Excel文件中读取.一旦我能够将Excel文件中的数据存储到字典中,我计划创建"Person"类的实例 - 虽然我不确定这是最好的方法......

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

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 …
Run Code Online (Sandbox Code Playgroud)

excel dictionary python-3.x openpyxl

1
推荐指数
1
解决办法
8453
查看次数

标签 统计

dictionary ×1

excel ×1

openpyxl ×1

python-3.x ×1