• 【python自动化框架搭建】路径处理,正则表达式(第22天课堂笔记)


    # -*- coding: utf-8 -*-
    
    import os
    
    # one_path = os.path.abspath(__file__)
    # two_path = os.path.dirname(one_path)
    # three_path = os.path.dirname(two_path)
    # 项目根路径
    BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
    
    # 获取配置文件所在的路径
    CONFIGS_DIR = os.path.join(BASE_DIR, 'configs')
    
    # 获取配置文件所在的路径
    CONFIG_FILE_PATH = os.path.join(CONFIGS_DIR, 'testcase.yaml')
    
    # 获取日志文件所在的目录路径
    LOGS_DIR = os.path.join(BASE_DIR, 'logs')
    
    # 获取报告文件所在的目录路径
    REPORTS_DIR = os.path.join(BASE_DIR, 'reports')
    
    # 获取excel文件所在的目录路径
    DATAS_DIR = os.path.join(BASE_DIR, 'datas')
    
    pass
    # -*- coding: utf-8 -*-
    
    import re
    
    # 正则表达式相当于一个模子, 可以拿这个模子去把符合这个模子的内容全部找出来
    from scripts.handle_mysql import HandleMysql
    
    # 1. 创建待替换的字符串
    one_str = '{"mobile_phone": "${not_existed_tel}", "pwd": "12345678", "type": 1, "reg_name": "KeYou"}'
    
    # 2. 创建正则表达式
    # 正则表达式中一定要加r, 如果有些字符有特殊含义, 需要在前面加一个
    # re_str = r'${not_existed_tel}'
    # match方法第一个参数为正则表达式, 第二个参数为待查询的字符串
    # match方法只能从头开始匹配
    # 如果匹配不上, 会返回None
    # 如果能匹配上, 会返回Match对象
    
    # mtch = re.match(r'${not_existed_tel}', one_str)
    # 可以使用mtch.group()获取匹配成功之后的值
    # mtch = re.match(r'{"mobile_phone": "${not_existed_tel}', one_str)
    
    # search
    # search方法,不用从头开始匹配,只要能匹配上就直接返回
    # 如果能匹配上,返回Match
    # 如果匹配不上,会返回None获取匹配成功之后的值
    # 可以使用mtch.group()
    # mtch = re.search(r"{not_existed_tel}",one_str)
    
    # sub
    # sub方法,第一个参数为正则表达式字符串,第二个参数为新的值(字符串)
    # 第三个参数为待替换的字符串(原始字符串)
    # 如果能匹配上,会返回替换之后的值(一定为字符串类型)
    # 如果匹配不上,会返回原始字符
    res = re.sub(r"{not_existed_tel}","18822223333",one_str)
    
    
    # 在项目中search方法和sub方法会合在一块用
    if re.search(r"{not_existed_tel}", one_str):
        res = re.sub(r"{not_existed_tel}", "18822223333", one_str)
    
    # split
    # findall
    # finditer
    
    do_mysql = HandleMysql()
    real_existed_tel = do_mysql.create_not_existed_mobile()
    
    do_mysql.close()
  • 相关阅读:
    android studio 使用(一)
    【转】How to Change File Ownership & Groups in Linux
    ubuntu 14.04安装nodejs
    基于源码学习-fighting
    linux shell操作
    linux用户管理
    ubutu强制结束进程 kill -9 ProcessID
    ubuntu 添加和删除用户
    ubuntu 14.04查看java的安装路径
    vuex 中关于 mapState 的作用
  • 原文地址:https://www.cnblogs.com/python-test001/p/12591204.html
Copyright © 2020-2023  润新知