Python面向对象编程7 excel基础操作

在这里插入图片描述

思路:
1.知道文件路径;
2.打开excel文件;
3.根据sheet,row,cell_value获取值,创建的数据对象中;
4.把创建的数据对象,追加到list列表中;
5.输出到打印台上;

实例1:
创建一个基础数据类用于存放对象数据

#基础数据信息类
class StudentBaseInfo():
    def __init__(self,stu_id,stu_name,stu_sex,stu_age,stu_score):
        self.stu_id = stu_id
        self.stu_name = stu_name
        self.stu_sex = stu_sex
        self.stu_age = stu_age
        self.stu_score = stu_score

    def show(self):
        pass

实例2:
打开excel文件,并获取数据,util类。

import os
import xlrd
from excel_read.student_base_info import StudentBaseInfo

def read_excel_get_studentinfo(file_path):
    workbook = xlrd.open_workbook(file_path)
    sheet = workbook.sheet_by_index(0)
    student_infos=[]
    for i in range(1,sheet.nrows):
        new01 = StudentBaseInfo(sheet.cell_value(i,0),
                                sheet.cell_value(i,1),
                                sheet.cell_value(i,2),
                                sheet.cell_value(i,3),
                                sheet.cell_value(i,4))
        student_infos.append(new01)
    return student_infos


if __name__ == '__main__':
    current_path = os.path.dirname(__file__)
    excel_path = current_path + '/../data/stu.xls'
    stus = read_excel_get_studentinfo(excel_path)
    print(stus[0].stu_name)

实例3:
调用封装方法

from excel_read import excel_utils
import os

current_path = os.path.dirname(__file__)
excel_path = current_path + '/../data/stu.xls'

stus = excel_utils.read_excel_get_studentinfo(excel_path)
print(stus[0].stu_name)
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值