一、安装依赖:
pandas
二、新建excel 示例数据:students.xlsx
三、定义类:student.py
Student
class Student:
def __init__(self, name, sex):
self.name = name
self.sex = sex
def show(self):
print(f'姓名:{self.name} 性别:{self.sex}')
四、读取excel内容并展示:excel.py
import pandas as pd
from student import Student
students = []
df = pd.read_excel('students.xlsx', sheet_name='Sheet1')
for index, row in df.iterrows():
s = Student(row["姓名"], row["性别"])
students.append(s)
for i in range(len(students)):
print(f'姓名:{students[i].name},性别:{students[i].sex}')
print('######################')
for i in range(len(students)):
students[i].show()
运行结果: