Python面向对象编程9 excel文件异常的一些处理方式

方法一:再try之前给列表赋值,返回一个空列表

import os
import xlrd
from case_info import CaseInfo


class ExcelDataInof:

    def __init__(self,file_path):
        self.file_path = file_path

    def read_data_in_class(self):
        all_case_info = []
        try:
            excel_file = xlrd.open_workbook(self.file_path)
            sheet = excel_file.sheet_by_index(0)
            for i in range(1,sheet.nrows):
                case_info = CaseInfo(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),
                                      sheet.cell_value(i,5),
                                      sheet.cell_value(i,6),
                                      sheet.cell_value(i,7))
                all_case_info.append(case_info)
        except FileNotFoundError as e:
            print('文件未找到')
        except IndexError as e:
            print('表格sheet下标越界')
        except Exception as e:
            print('系统异常:'+str(e))
        return  all_case_info

if __name__ == '__main__':
    cerrunt_path = os.path.dirname(__file__)
    file_path = cerrunt_path + '/../test_case/test_c1ase.xls'
    Excel_data = ExcelDataInof(file_path)
    str1 = Excel_data.read_data_in_class()
    print(str1[0])

方法二:try无法读取路径下,则默认去读默认文件

import os
import xlrd
from case_info import CaseInfo


class ExcelDataInof:

    def __init__(self,file_path):
        self.file_path = file_path

    def read_data_in_class(self):
        all_case_info = []
        try:
            excel_file = xlrd.open_workbook(self.file_path)
        except FileNotFoundError as e:
            cerrunt_path = os.path.dirname(__file__)
            file_path = cerrunt_path + '/../test_case/test_case.xls'
            excel_file = xlrd.open_workbook(file_path)
            print('文件未找到')
        sheet = excel_file.sheet_by_index(0)
        for i in range(1,sheet.nrows):
            case_info = CaseInfo(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),
                                  sheet.cell_value(i,5),
                                  sheet.cell_value(i,6),
                                  sheet.cell_value(i,7))
            all_case_info.append(case_info)

        return  all_case_info

if __name__ == '__main__':
    cerrunt_path = os.path.dirname(__file__)
    file_path = cerrunt_path + '/../test_case/test_1c1ase.xls'
    Excel_data = ExcelDataInof(file_path)
    str1 = Excel_data.read_data_in_class()
    print(str1[0].act_results)

方法三:第三方模块自带自定义异常

目的,就是为了二次封装的时候能封装更多自定义异常(百度-英文官方文档)
举例:
https://pypi.org/中搜索相关包(xlrd)
在这里插入图片描述
进入xlrd:
在这里插入图片描述
进入Homepage
在这里插入图片描述
进入xlrd的Documentation:
在这里插入图片描述
进入API模块查看使用方法:
在这里插入图片描述
from xlrd.compdoc import CompDoc
from xlrd.biffh import XLRDError

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值