zoukankan      html  css  js  c++  java
  • 读取excel,类封装(4)

    #基础用例数据类
    class CaseInfo:
    def __init__(self,case_id,case_name,case_module,case_pri,case_step,case_result):
    self.case_id=case_id
    self.case_name=case_name
    self.case_module=case_module
    self.case_pri=case_pri
    self.case_step=case_step
    self.case_result=case_result

    #类封装
    import os
    import xlrd
    from pritices_daily import case_infos
    class ReadExcel:
    def __init__(self,excel_path):
    self.excel_path=excel_path

    def read_excel_data_convert_info_01(self):
    # 1,读取数据存到列表中:[[用例编号,用例名称,所在模块...],[....],[......]]
    workbook = xlrd.open_workbook(self.excel_path) # 打开工作簿
    sheet = workbook.sheet_by_index(0) # 打开第一个表
    all_case_info1 = []
    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_info1.append(case_info)
    return all_case_info1

    # 2,做成类形成
    def read_excel_data_convert_info_02(self):
    workbook = xlrd.open_workbook(self.excel_path) # 打开工作簿
    sheet = workbook.sheet_by_index(0) # 打开第一个表
    all_case_info2 = []
    for i in range(1, sheet.nrows):
    case_id = sheet.cell_value(i, 0)
    case_name = sheet.cell_value(i, 1)
    case_module = sheet.cell_value(i, 2)
    case_pri = sheet.cell_value(i, 3)
    case_step = sheet.cell_value(i, 4)
    case_result = sheet.cell_value(i, 5)
    CaseInfo = case_infos.CaseInfo(case_id, case_name, case_module, case_pri, case_step, case_result)
    all_case_info2.append(CaseInfo)
    return all_case_info2[0].case_name

    if __name__ == '__main__':
    current_path = os.path.dirname(__file__)
    excel_path = os.path.join(current_path, '../data/testcases.xlsx')
    rd01=ReadExcel(excel_path)
    print(rd01.read_excel_data_convert_info_01())
    print(rd01.read_excel_data_convert_info_02())
  • 相关阅读:
    阿里云云计算认证ACP模拟考试练习题10套:第1套模拟题分享
    as3+java+mysql(mybatis) 数据自动工具(二)
    自动化登录账号密码并截图保存
    自动化读取csv文件
    自动化读取磁盘文件并逐一在百度上面进行搜索
    自动化打开百度搜索
    python自动化--数据驱动(从文件中提取)
    pycharm 命令行方式
    软件测试工程师 总结
    八大元素定位代码
  • 原文地址:https://www.cnblogs.com/tingting-yang/p/13262224.html
Copyright © 2011-2022 走看看