模块

今日内容写详细

取消转义

在正则表达式中取消转义推荐使用\(每个\只能取消一个字符的转义)
在python中取消推荐转义建议用r'\n'

python内置模块之re模块和其它模块

[]里面的特殊符号\    叫转义符  最牛的一个斜杠

\d   匹配任意十进制数,相当于[0-9]

\D   匹配任意非数字字符,相当于[^0-9]

\s   匹配任何空白字符,相当于[\t\n\r\f\v]

\S   匹配任何非空白字符,相当于[^\t\n\r\f\v]

\w   匹配任何字符数字字符,相当于[a-zA-Z0-9_]

\W   匹配任何非字母数字字符,相当于[^a-zA-Z0-9_]
    re.findall("a","a bb") 返回所有满足条件的结果,放在列表里面
    re.findall("abc","abccccc")

    ['abc']
    re.search("a","a bb c").group()取出来是一个对象

	# 匹配不到则返回空, 匹配到第一个之后就不继续往下匹配了
	re.search("\d{2}","abcccc9879").group()

	'98'
    
    re.match("a","abc").group()与search相同,只不过仅在字符串开始出进行匹配
    复制代码

    re.match("abc","abccccc")

	<_sre.SRE_Match object; span=(0, 3), match='abc'>

    re.match("abc","abccccc").group()

    re.search("(abc)","abccccc")
    <_sre.SRE_Match object; span=(0, 3), match='abc'>
    
    re.search("\d{2}","abccccc")  # 匹配不到则返回空
    
    re.search("\d{2}","abcccc9879")  # 取出来是一个对象,想要值,用group方法
    <_sre.SRE_Match object; span=(6, 8), match='98'>
    
    re.search("\d{2}","abcccc9879").group()
    '98'
    re.split("[ab]","asdabcd")

	['', 'sd', '', 'cd']
    
     re.sub("\d+","A","sdfdsfgc56712MMns980")

	'sdfdsfgcAMMnsA'
    re.subn("\d","A","sdfdsfgc56712MMns980")

	('sdfdsfgcAAAAAMMnsAAA', 8)
    
    com=re.compile("\d+")

    com.findall("sdcvf456dfg67")
    
    ['456','67']
    
    re.findall("\d","sdcvf456dfg67")

	['4', '5', '6', '6', '7']
    
    re.finditer("\d","sdcvf456dfg67")

	<callable_iterator object at 0x0000000000D92160>
    ret = re.finditer("\d", "sdcvf456dfg67")
	print(next(ret).group())  # 4
	print(next(ret).group())  # 5
    # 当有分组的时候findall优先匹配组里面的内容

    ret=re.findall("www\.(baidu|163)\.com","www.baidu.com")

    re.findall("www\.(baidu|163)\.com","www.baidu.com")

	['baidu']
	# 也可以加个?:去掉优先级

    re.findall("www\.(?:baidu|163)\.com","www.baidu.comaawww.163.combv")

	['www.baidu.com', 'www.163.com']

collections模块

# 该模块内部提供了一些高阶的数据类型

1.namedtuple(具名元组)
    from collections import namedtuple

    """
    namedtuple('名称',[名字1,名字2,...])
    namedtuple('名称','名字1 名字2 ...')
    """
    # point = namedtuple('坐标', ['x', 'y'])
    # res = point(11, 22)
    # print(res)  # 坐标(x=11, y=22)
    # print(res.x)  # 11
    # print(res.y)  # 22
    # point = namedtuple('坐标', 'x y z')
    # res = point(11, 22, 33)
    # print(res)  # 坐标(x=11, y=22, z=33)
    # print(res.x)  # 11
    # print(res.y)  # 22
    # print(res.z)  # 33
    # card = namedtuple('扑克', '花色 点数')
    # card1 = card('♠', 'A')
    # card2 = card('♥', 'K')
    # print(card1)
    # print(card1.花色)
    # print(card1.点数)
2.队列
    # 队列模块
    import queue  # 内置队列模块:FIFO
    # 初始化队列
    # q = queue.Queue()
    # 往队列中添加元素
    # q.put('first')
    # q.put('second')
    # q.put('third')
    # 从队列中获取元素
    # print(q.get())
    # print(q.get())
    # print(q.get())
    # print(q.get())  # 值去没了就会原地等待
3.双端队列
    from collections import deque
    q = deque([11,22,33])
    q.append(44)  # 从右边添加
    q.appendleft(55)  # 从左边添加
    print(q.pop())  # 从右边取值
    print(q.popleft())  # 从做边取值
4.有序字典
    normal_dict = dict([('name', 'jason'), ('pwd', 123), ('hobby', 'study')])
    print(normal_dict)
    {'hobby': 'study', 'pwd': 123, 'name': 'jason'}
    from collections import OrderedDict
    order_dict = OrderedDict([('name', 'jason'), ('pwd', 123), ('hobby', 'study')])
    print(order_dict)
    OrderedDict([('name', 'jason'), ('pwd', 123), ('hobby', 'study')])
    order_dict['xxx'] = 111
    order_dict
    OrderedDict([('name', 'jason'), ('pwd', 123), ('hobby', 'study'), ('xxx', 111)])
    normal_dict['yyy'] = 222
    normal_dict
    {'hobby': 'study', 'pwd': 123, 'yyy': 222, 'name': 'jason'}
5.默认值字典
	from collections import defaultdict
    values = [11, 22, 33, 44, 55, 66, 77, 88, 99, 90]
    my_dict = defaultdict(list)
    for value in  values:
        if value>60:
            my_dict['k1'].append(value)
        else:
            my_dict['k2'].append(value)
    print(my_dict)
6.计数器
	res = 'abcdeabcdabcaba'
    # 统计字符串中每个元素出现的次数
    # new_dict = {}
    # for i in res:
    #     if i not in new_dict:
    #         new_dict[i] = 1
    #     else:
    #         new_dict[i] += 1
    # print(new_dict)
    from collections import Counter  # 计数器
    ret = Counter(res)
    print(ret)

time模块

"""
时间三种表现形式
	1.时间戳(秒数)
	2.结构化时间(一般是给机器看的)
	3.格式化时间(一般是给人看的)
	三种时间是可以相互转换的!!!
"""
1.time.sleep()  # 原地阻塞指定的秒数
2.time.time()  # 获取时间戳时间

import time


# 格式化时间
# print(time.strftime('%Y-%m-%d'))  # 2021-11-25
# print(time.strftime('%Y-%m-%d %H:%M:%S'))  # 2021-11-25 11:48:34
# print(time.strftime('%Y-%m-%d %X'))  # 2021-11-25 11:48:34
"""
更多时间相关符号 保存到容易查找的位置即可
"""
# print(time.localtime())
# time.struct_time(
# tm_year=2021,
# tm_mon=11,
# tm_mday=25,
# tm_hour=11,
# tm_min=51,
# tm_sec=25,
# tm_wday=3,
# tm_yday=329,
# tm_isdst=0)


# print(time.time())
print(time.gmtime(11111111111))
# print(time.localtime())

datetime模块

import datetime
# print(datetime.date.today())  # 2021-11-25
# print(datetime.datetime.today())  # 2021-11-25 12:15:11.969769
"""date年月日  datetime年月日时分秒  time时分秒(MySQL django后期可以)"""
# res = datetime.datetime.today()
# print(res.year)  # 2021
# print(res.month)  # 11
# print(res.day)  # 25
# print(res.weekday())  # 获取星期(weekday星期是0-6) 0表示周一
# print(res.isoweekday())  # 获取星期(weekday星期是1-7) 1表示周一
"""时间差(timedelta)"""
# ctime = datetime.datetime.today()
# time_tel = datetime.timedelta(days=3)
# print(ctime)  # 2021-11-25 12:20:48.570489
# print(ctime - time_tel)  # 2021-11-22 12:21:06.712396
# print(ctime + time_tel)  # 2021-11-28 12:21:06.712396
"""
日期对象 = 日期对象 +/- timedelta对象
timedelta对象 = 日期对象 +/- 日期对象
"""
# ret = ctime + time_tel
# print(ret - ctime)  # 3 days, 0:00:00
# print(ctime - ret)  # -3 days, 0:00:00


# 小练习 计算举例今年过生日还有多少天
# birthday = datetime.date(2000, 11, 11)
# now_date = datetime.date.today()
# days = birthday - now_date
# print('距离生日还有{}天'.format(days))

# UTC时间与我们的东八区时间差 八个小时
# print(datetime.datetime.now())  # 2021-11-25 12:25:33.579310
# print(datetime.datetime.utcnow())  # 2021-11-25 04:25:33.579310
posted @ 2021-11-25 19:44  特烦恼  阅读(124)  评论(0)    收藏  举报