• python之常用的内置模块以及正则表达式的使用


    Python官方提供的模块,就叫做内置模块。
    主要包含:日期时间模块,数学计算模块,正则表达式模块。

    一数学计算模块math

    import math

    a=math.ceil(2.4)
    b=math.floor(2.4)
    c=math.pow(4,2)
    d=math.degrees(0.5*math.pi)
    f=math.radians(180/math.pi)
    print(“{0} {1} {2} {3} {4}”.format(a,b,c,d,f))

    在这里插入图片描述

    二日期时间模块datetime

    datetime:包含时间和日期
    date:只包含日期
    time:只包含时间
    timedelta:计算时间跨度
    tzinfo:时区信息

    三正则表达式模块re

    正则表达式指预定好一个“字符串模板”,通过这个“字符串模板”可以匹配查找和替换那些匹配“字符串模板”的字符串。

    3.1字符串匹配

    import re
    p = r"\w+@robsence.com"
    email = “Alex_wang0001@robosence.com”
    m = re.match(p,email)
    type(m)
    print(m)

    3.2字符串查找

    import re
    p = r"\w+@robsence.com"
    text = “email is Alex_wang0001@robosence.com.”
    m = re.search(p,text)

    print(m)

    3.2字符串替换

    正则表达式的字符串替换函数是sub(),该函数替换匹配的字符串,返回值是替换之后的字符串,其语法格式如下:
    re.sub(pattern,repl,string,count=0)
    import re
    p = r"\d+"
    text = “AAVV45CD78EER”
    replace_text = re.sub(p," ",text)
    print(replace_text)

    replace_text = re.sub(p," ",text,count=1)
    print(replace_text)

    replace_text = re.sub(p," ",text,count=2)
    print(replace_text)
    在这里插入图片描述

    3.3字符串分割

    在python中 使用re 模块中的split()函数进行字符串分割,该函数按照匹配的子字符串进行字符串分割,该函数按照匹配的子字符串进行分割,返回字符串列表对象,其语法格式如下:
    re.split(pattern,string,maxsplit=0)
    import re
    p = r"\d+"
    text = “AAVV45CD78EER”
    replace_text = re.split(p,text)
    print(replace_text)

    replace_text = re.split(p,text,maxsplit=1)
    print(replace_text)

    replace_text = re.split(p,text,maxsplit=2)
    print(replace_text)
    在这里插入图片描述

  • 相关阅读:
    认识thinkphp框架
    力扣热题100_矩阵_240_搜索二维矩阵 II
    Maven 配置指南
    Linux 查找动态库位置
    【数据结构】数组和广义表
    【Python从入门到进阶】58、Pandas库中Series对象的操作(一)
    京东按图搜索京东商品(拍立淘) API (.jd.item_search_img)快速抓取数据
    嵌入式汇编大合集
    ASEMI肖特基二极管SB30100LCT图片,SB30100LCT应用
    LoadRunner——分析图详解(十四)
  • 原文地址:https://blog.csdn.net/qq_35968965/article/details/126255598