栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

自动化(八)Python进阶笔记

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

自动化(八)Python进阶笔记

Python常用方法内容系统性整合
  • 一、Excel数据处理


Python自动化过程中会使用到的一些进阶方法

一、Excel数据处理

1、表格定位
①文件路径【file_path】,可直接赋值
②sheet名称【sheet_name】,可直接赋值


2、数据获取
①目标赋值:test_dict[“id”] = sheet.cell(i, 1).value
②加入字典:test_excel_data.append(test_dict)

class DoExcel:
    def __init__(self, file_path, sheet_name):
        self.file_path=file_path
        self.sheet_name=sheet_name

    def get_data(self):
        wb = load_workbook(self.file_path)
        sheet = wb[self.sheet_name]
        test_excel_data = []
        for i in range(2, sheet.max_row+1):
            test_dict = {}
            test_dict["case_id"] = sheet.cell(i, 1).value
            test_dict["case_name"] = sheet.cell(i, 2).value
            test_dict["method"] = sheet.cell(i, 3).value
            test_dict["url"] = sheet.cell(i, 4).value
            test_dict["param"] = sheet.cell(i, 5).value
            test_excel_data.append(test_dict)
        return test_excel_data

3、数据写入

class DoExcel:
    def __init__(self, file_path, sheet_name):
        self.file_path=file_path
        self.sheet_name=sheet_name
        
    def write_data(self, r, c, value):
        wb = load_workbook(self.file_path)
        sheet = wb[self.sheet_name]
        sheet.cell(r, c).value = value
        wb.save(self.file_path)

4、调式验证
可以用调试一下测试是否可以执行成功

if __name__ == "__main__":
    file_path = 'G:mine_apitest_caseapi_test.xlsx'
    sheet_name = 'business_collect'
    test_excel_data=DoExcel(file_path, sheet_name).get_data()
    print(test_excel_data)
    test_excel_data=DoExcel(file_path, sheet_name).write_data()	#api_test.xlsx看写入结果

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/1011813.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号