zoukankan      html  css  js  c++  java
  • python自动化之读取excel文件内容请求接口生成结果重新写入新的excel文件

    流程步骤:

      1、用excel文件编写好测试用例;

      2、通过python脚本读取excel文件;

      3、读取的excel文件内容去请求接口;

      4、将请求接口返回的结果&旧excel文件内容写入到新的excel文件中。

    import requests
    import json,xlrd
    from xlwt import Workbook
    
    def api_checkwarning(words):
        url = "http://ip:port/api/realtime/checkwarning"
        headers = {
            'Content-Type': 'application/json'
            }
        data = {
            "role": "2",
            "words": "85731928",
            "agent_id": "72123"
            }
    
        data["words"] = str(words)
    
        response = requests.post(url=url, headers=headers, data=json.dumps(data))
        result = response.json()
        return result
    
    
    if __name__ == "__main__":
        filename_1 = "Cardcenter.xlsx"
        filename_2 = "Cardcenter_results.xlsx"
        book = Workbook()
        sheet1 = book.add_sheet('checkwarning')
        sheet1.write(0, 0, "ID")
        sheet1.write(0, 1, "规则")
        sheet1.write(0, 2, "测试点(传参)")
        sheet1.write(0, 3, "测试结果")
        sheet1.write(0, 4, "是否通过")
        bk=xlrd.open_workbook(filename_1)
        sh = bk.sheet_by_name("checkwarning")
        num = sh.nrows
        for i in range(1,num):
            num_1 = 0
            id = sh.cell_value(i, 0)
            sheet1.write(i, 0, id)
            rule = sh.cell_value(i, 1)
            sheet1.write(i, 1, rule)
            words = sh.cell_value(i, 2)
            sheet1.write(i, 2, words)
            result = api_checkwarning(words)
            sheet1.write(i, 3, str(result))
            list_1 = result["data"]["CheckResults"]
            if list_1 == []:
                sheet1.write(i, 4, "质检结果为空")
            elif list_1 is None:
                sheet1.write(i, 4, "返回None")
            else:
                for j in range(len(list_1)):
                    if list_1[j]['ruleId'] == id:
                        num_1 += 1
                    else:
                        pass
                if num_1 == 1:
                    sheet1.write(i, 4, "通过")
                elif num_1 >=2:
                    sheet1.write(i, 4, "命中告警规则重复")
                else:
                    sheet1.write(i, 4, "命中告警规则失败")
        book.save('Cardcenter_results.xlsx')
        print("结束")
    

      

  • 相关阅读:
    js定时跳转
    MySQL跨表更新字段 工作记录
    windows下安装phpcms html/ 文件夹不可写的一种错误以及解决方法
    linux清理僵尸进程
    JQuery实现隔行变色和突出显示当前行 效果
    windows下配置lamp环境(4)---安装MySQL数据库5.6
    windows下配置lamp环境(5)---配置MySQL5.6
    windows下配置lamp环境(3)---配置PHP5.4
    windows下配置lamp环境(0)---软件获取
    windows下配置lamp环境(2)---配置Apache服务器2.2.25
  • 原文地址:https://www.cnblogs.com/mtfan01/p/13591911.html
Copyright © 2011-2022 走看看