python读取excel文件代码封装_python应用_读取Excel数据【二】_二次封装之函数式封装...

目的:想要把对Excel文件读取做成一个通用的函数式封装,便于后续简单调用,隔离复杂性。

未二次封装前原代码:

#coding=gbk

import os

import xlrd

current_path=os.path.dirname(__file__)

excel_path=os.path.join(current_path,'../testcase.xlsx')

workbook=xlrd.open_workbook(excel_path)

sheet=workbook.sheet_by_index(0)

all_case_info = []

for i in range(1,sheet.nrows):

case_info = []

for j in range(0,sheet.ncols):

case_info.append(sheet.cell_value(i,j))

all_case_info.append(case_info)

print(all_case_info)

更改后:

原文件:

#coding=gbk

import os

import xlrd

current_path=os.path.dirname(__file__)

excel_path=os.path.join(current_path,'../testcase.xlsx')

def read_excel_date_convert_case_info(excel_path):

workbook = xlrd.open_workbook(excel_path)

sheet = workbook.sheet_by_index(0)

all_case_info = []

for i in range(1,sheet.nrows):

case_info = []

for j in range(0,sheet.ncols):

case_info.append(sheet.cell_value(i,j))

all_case_info.append(case_info)

return all_case_info #一定要有返回

#顶层代码调试

if __name__ == '__main__':

current_path = os.path.dirname(__file__)

excel_path = os.path.join(current_path, '../testcase.xlsx')

cases=read_excel_date_convert_case_info(excel_path)

print(cases)

调用:

#coding=gbk

import os

from test0418.demo02 import read_excel_date_convert_case_info #引入对应的函数

current_path = os.path.dirname(__file__)

excel_path = os.path.join(current_path, '../testcase.xlsx')

cases = read_excel_date_convert_case_info(excel_path) #别人调用时候只需要import该函数,然后直接

print(cases) #检测输出结果

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值