1.怎么在re模块中使用search给要输出的内容进行命名:(?p<name>正则表达式)
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
import re ret=re.search('d(?P<name>w+)','1jdkfjjdfkjjfdj') print(ret.group('name')) 结果为 jdkfjjdfkjjfdj
2.可以利用分组中的的形式给分组进行起名字:(?P<name>)
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
import re ret=re.search('<(?P<tag_name>w+)>w+</(?P=tag_name)>','<h1>hello</h11>') print(ret.group('tag_name'))#如果前后的tag_name不一致则会报错 print(ret.group()) 结果为 File "D:/python练习程序/第十七天/第十九天.py", line 10, in <module> print(ret.group('tag_name')) AttributeError: 'NoneType' object has no attribute 'group' import re ret=re.search('<(?P<tag_name>w+)>w+</(?P=tag_name)>','<h1>hello</h1>') print(ret.group('tag_name'))#如果前后的tag_name不一致则会报错 print(ret.group()) 结果为 h1 <h1>hello</h1>
3.如果不给分组中起名字也可以通过序号来找到对应的组:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
import re ret = re.search(r"<(w+)>w+</1>","<h1>hello</h1>") print(ret.group(1)) print(ret.group()) 结果为 h1 <h1>hello</h1>
4.寻找计算字符串中的整数:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
import re ret=re.findall(r'd+.d+|(d+)','1-2*(60+(-40.35/5)-(-4*3))') print(ret) ret.remove('') print(ret) 结果为 ['1', '2', '60', '', '5', '4', '3'] ['1', '2', '60', '5', '4', '3']
5.如果要把数字里的符号带着:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
import re ret=re.findall(r'-?d+.d+|(-?d+)','1-2*(60+(-40.35/5)-(-4*3))') print(ret) ret.remove('') print(ret) 结果为 ['1', '-2', '60', '', '5', '-4', '3'] ['1', '-2', '60', '5', '-4', '3']
6.flag的许多用法:
7.匹配一段文本中的每行的邮箱:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
import re ret=re.findall(r'(?P<email>w+://w+.w+.w*?/)','http://blog.csdn.net/make164444332/artciel/5444') print(ret) 结果为 ['http://blog.csdn.net/']
8.正则表达式的一些题目
9.模块的讲解:collections 、random、序列化、os、时间模块、
10.1collectios模块:python中的扩展数据类型:
10.1namedtuple :生成的名字可以访问元素的tuple
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
from collections import namedtuple Point=namedtuple('point',['x','y','z']) p1=Point(1,3,5) p2=Point(2,3,6) print(p1) print(p1.x) print(p2.z) 结果为 point(x=1, y=3, z=5) 1 6
10.2deque:双端队列,可以快速的从另外一端追加和推出对象:使用列表进行数据存储时如果按照索引进行元素访问时,就会很快,但是插入和删除元素就会很慢,因为list时线
性存储而队列是链性存储
10.21队列:满足先进先出的原则:FIFO
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
import queue q=queue.Queue() q.put([1,3,5]) q.put(3) q.put((1,4)) print(q.get()) print(q.get()) print(q.get()) print(q.get())#当值取完之后在操作不会报错会进行堵塞不会进行接下来的操作 q.put(3) print(q.get()) 结果为 [1, 3, 5] 3 (1, 4)
10.22双端队列指的是数据既可以从前面、后面或者插入的方式进行数据的放入和读取:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
from collections import deque dq=deque([1,3]) dq.append('a')#在队列的最后添加元素 dq.appendleft('b')#在队列的最前面添加元素 dq.insert(3,4)#在队列的最4个位置添加元素 print(dq) print(dq.pop())#取出最后一个元素 print(dq.popleft())#取出第一个元素 结果为 deque(['b', 1, 3, 4, 'a']) a b
10.3有序字典:就是按照key的顺序进行输出:常用于购物车或者一些有序的场景下,但是有序字典比无序字典占的内存大:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
from collections import OrderedDict od=OrderedDict([('a',1),('b',4),('c',5)]) for i in od: print(i) 结果为 a b c
10.4带默认值的字典:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
from collections import defaultdict d=defaultdict(lambda:5)#必须是一个callable类型 print(d['k']) 结果为 5
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
from collections import defaultdict values=[11,33,55,66,77,99,134,43] my_dict=defaultdict(list) for value in values: if value>66: my_dict['k1'].append(value) else: my_dict['k2'].append(value) print(my_dict) 结果为 defaultdict(<class 'list'>, {'k2': [11, 33, 55, 66, 43], 'k1': [77, 99, 134]})
11.time模块:
在python中又三种方式来表示时间分别是时间戳、元组(strct_time)、格式化字符串
11.1时间戳(struct_stamp):通常来说,时间戳就是从1970年1月1号开始·计算时间的偏移量,类型是浮点型(float)
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
import time ret=time.time() print(ret) 结果为 1582203205.305226
11.2格式化的·时间字符串:formatstirng
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
import time print(time.strftime('%Y-%m-%d %H:%M:%S'))#year month day hour minute second 结果为 2020-02-20 20:56:56
11.3元组(struct_time)里面共有9个元素(年月日时分秒一年中第几周,一年中第几天)
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
import time struct_time=time.localtime() print(struct_time) print(struct_time.tm_yday) 结果为 time.struct_time(tm_year=2020, tm_mon=2, tm_mday=20, tm_hour=21, tm_min=4, tm_sec=1, tm_wday=3, tm_yday=51, tm_isdst=0) 51
11.4 3种格式的转换:
11.31时间戳和结构化时间的转换:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
import time t=time.time() print(t) print(time.localtime(t)) 结果为 1582204284.5429978 time.struct_time(tm_year=2020, tm_mon=2, tm_mday=20, tm_hour=21, tm_min=11, tm_sec=24, tm_wday=3, tm_yday=51, tm_isdst=0)
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
import time t=time.localtime() print(t) print(time.mktime(t)) 结果为 time.struct_time(tm_year=2020, tm_mon=2, tm_mday=20, tm_hour=21, tm_min=13, tm_sec=24, tm_wday=3, tm_yday=51, tm_isdst=0) 1582204404.0
11.34时间格式化和时间结构化之间的转换:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
import time ret=time.strftime('%Y-%m-%d %H:%M:%S') print(time.strptime(ret,'%Y-%m-%d %H:%M:%S')) 结果为 time.struct_time(tm_year=2020, tm_mon=2, tm_mday=20, tm_hour=21, tm_min=24, tm_sec=14, tm_wday=3, tm_yday=51, tm_isdst=-1)
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
print(time.strftime('%Y-%m-%d %H:%M:%S',time.localtime())) 结果为 2020-02-20 21:25:41
11.35
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
import time t=time.time() print(time.localtime(t)) print(time.ctime(t)) 结果为 time.struct_time(tm_year=2020, tm_mon=2, tm_mday=21, tm_hour=8, tm_min=58, tm_sec=6, tm_wday=4, tm_yday=52, tm_isdst=0) Fri Feb 21 08:58:06 2020
12random模块:
12.1随机整数模块:raindint
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
import random ret=random.randint(1,5)#大于等于1小于等于5之间的数 print(ret) ret=random.randint(1,5) print(ret) 结果为 3 4
12.2随机的范围randrange
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
import random ret=random.randrange(1,12,2)#大于等于1小于等于5之间的数 print(ret) 结果为 3
12.3随机选则一个返回:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
import random ret=random.choice((1,12,['fjkj'],23))#里面可以是元组和列表 print(ret) 结果为 ['fjkj']
12.4随机多个值进行返回,返回的个数又第二个参数确定:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
import random ret=random.sample((1,12,['fjkj'],1,344,'fff',555,23),2)#里面可以是元组和列表 print(ret)#以列表的形式返回 结果为 [555, 23]
12.5打乱顺序
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
import random item=[1,3,5,7,9] random.shuffle(item) print(item) 结果为 [9, 1, 3, 7, 5]
13.os操作:和操作系统相关:
13.1获取文件的当前文件目录:getcwd
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
import os print(os.getcwd()) 结果为 D:python练习程序第十七天
13.2改变脚本当前的目录:chdir
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
# print(ret) import os os.chdir('D:python练习程序') print(os.getcwd()) 结果为 D:python练习程序
133返回当前的目录:os.curdir
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
import os print(os.curdir) print(os.getcwd()) 结果为 . D:python练习程序第十七天
13.4返回上一层目录:pardir
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
import os ret=os.pardir print(ret) print(os.getcwd()) 结果为 .. D:python练习程序第十七天
13.5可以生成多层递归目录:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
import os os.makedirs('dirmake1/dirmake2') print(os.getcwd()) 结果为 你可以在文件栏看到新创建的文件如果新创建的文件存在则会报错
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
import os os.removedirs('dirmake1/dirmake2')#会进行递归删除文件知道文件不为空才停止
13.6生成单极目录:mkdir
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
import os os.mkdir('dirmake1')#生成单层目录 结果为
13.7删除单层目录:rmdir
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
import os os.rmdir('dirmake1')#删除单层目录
13.8列出指定目录下的所有文件和子目录:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
import os ret=os.listdir('D:/')#删除单层目录 print(ret) 结果为 ['$RECYCLE.BIN', '-VISO', '1223', '4', '66', 'a72b0ed27408eb954ae4ad848795504e26b34ea2.pdf', 'addd', 'BaiduNetdiskDownload', 'cad', 'caj', 'Config.Msi', 'dd', 'dnf', 'DNF1', 'Dosame', 'gxworks2', 'HLMJ', 'idm下载', 'matalab', 'matytype', 'MSOCache', 'notepad', 'pagefile.sys', 'Program Files (x86)', 'PSAutoRecover', 'ps软件', 'pycharm5.0.3', 'python', 'python 程序', 'python11', 'python3.5', 'python练习程序', 'pyton视频', 'QLDownload', 'QLDownloadGame', 'QQ', 'QQLive', 'qq文件', 'qq游戏', 'start', 'System Volume Information', 'test1.txt', 'test2.docx', 'test2.txt', 'test3.txt', 'test4.txt', 'test6,txt', 'test6.txt', 'test7.txt', 'tgp', 'ThunderX_28775', 'tuppy', 'untitled', 'u盘东西', 'viso保存', 'viso图形包', 'winr', 'word保存文件', 'Youku Files', '下载', '中国机长.ts', '优秀软件', '优酷截屏文件', '优酷文件下载', '冰点文库下载器', '图库资源', '好听歌曲下载', '好的软件', '思维导图', '搞笑.ts', '新建文件夹', '新建文件夹 (2)', '欧阳孟可.doc', '歌曲下载', '爱奇艺', '电影', '电视', '百度云', '知乎', '神奇脑波', '腾讯视频缓存', '英语6级', '蛋蛋VIP视频全网解析_5.ts', '街机游戏', '视频录制', '解密软件', '资料', '迅雷', '酷我下载', '酷我缓存', '金山打字法', '金山打字法.exe', '镇魂街.txt', '镇魂街1.txt', '麻将', '鼠标驱动']
13.8给文件重新命名;rename
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
import os ret=os.rename('file','file.bak')#前面是旧的,后面是新的 print(ret)
13.9os.path
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
import os.path ret=os.getcwd() print(os.path.abspath('第十九天.py')) print(os.path.split(ret)) 结果为 D:python练习程序第十七天第十九天.py ('D:\python练习程序', '第十七天')
13.10判断是否是相对路径:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
import os.path ret=os.getcwd() ret1=os.path.abspath('第十九天.py') print(os.path.isabs(ret1)) 结果为 True
13.11判断文件是否为文件夹:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
import os ret=os.getcwd() ret1=os.path.abspath('第十九天.py') print(os.path.isdir('c1')) 结果为 True
13.12将多个路径进行组合后返回:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
import os ret=os.getcwd() print(os.path.getsize('file1')) 结果为 41
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
import os ret=os.getcwd() print(ret) ret1=os.path.join('D:','python练习程序','第十七天','file1') print(ret1) 结果为 D:python练习程序第十七天 D:python练习程序第十七天file1
13.13文件的目录读取:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
import os ret=os.popen('dir').read() print(ret) 结果是 2020/02/21 18:44 <DIR> . 2020/02/21 18:44 <DIR> .. 2020/02/21 18:43 <DIR> .idea 2020/02/21 11:36 <DIR> c1 2020/02/18 19:14 369 file.bak 2020/02/21 11:42 41 file1 2020/02/18 20:59 0 file1.txt 2020/02/18 19:17 169,579 websize 2020/02/21 13:07 4,254 作业.py
14.sys操作:
14.1电脑版本的判别:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
import sys print(sys.platform) 结果为 win32
14.2判断·python的型号:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
import sys print(sys.version) 结果为 3.6.3 |Anaconda, Inc.| (default, Oct 15 2017, 03:27:45) [MSC v.1900 64 bit (AMD64)]
14.3返回文件的搜索路径,初始化时使用pythonpath的环境变量的值:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
import sys print(sys.path) 结果为 ['D:\python练习程序\第十七天', 'D:\python练习程序\第十七天', 'C:\pycharm\python36.zip', 'C:\pycharm\DLLs', 'C:\pycharm\lib', 'C:\pycharm', 'C:\pycharm\lib\site-packages', 'C:\pycharm\lib\site-packages\Babel-2.5.0-py3.6.egg', 'C:\pycharm\lib\site-packages\win32', 'C:\pycharm\lib\site-packages\win32\lib', 'C:\pycharm\lib\site-packages\Pythonwin']
14.4可以在命令框进行操作:argv