1.模块

a.定义:本质就是.py结尾的python文件,逻辑上组织python代码,实现某种功能。例:文件名test.py-->模块名test。

b.导入方法:imort moduname

from mdname import *

from mdname import name as rename

...

c.import本质(路径搜索和搜索路径)

d.导入优化:from mdname import test

e.模块分类:标准库内的模块、开源模块、自定义模块。

2.time &datetime模块

  Python的时间模块主要有两个:time和datetime。

2.1 time模块

  Python的time模块实现主要调用C库,所以各个平台可能有所不同。time模块表示时间的有三种类型:

  (1)timestamp 时间戳,时间戳表示的是从1970年1月1日00:00:00开始按秒计算的偏移量。返回时间戳方式的函数主要有time(),clock()等;

  (2)struct_time 时间元组,共有九个元素。返回struct_time的函数主要有gmtime(),localtime(),strptime();

  1. tm_year 2018
  2. tm_mon 1 12
  3. tm_mday 1 31
  4. tm_hour 0 23
  5. tm_min 0 59
  6. tm_sec 0 61 (6061 是闰秒)
  7. tm_wday 06 (0是周一)
  8. tm_yday 1 366(儒略历)
  9. tm_isdst -1, 0, 1, -1是决定是否为夏令时的旗帜

  (3)format time 格式化时间,自定义格式和固定格式。相当于把时间转换成可读性比较高的字符串。

补充:UTC(Coordinated Universal Time,世界协调时)亦即格林威治天文时间,世界标准时间。中国北京时间(东八区)为UTC+8。DST(Daylight Saving Time)为夏令时。

  1. #_*_coding:utf-8_*_
  2. import time
  3.  
  4. # print(time.clock()) #返回处理器时间,3.3开始已废弃 , 改成了time.process_time()测量处理器运算时间,不包括sleep时间,不稳定,mac上测不出来
  5. # print(time.altzone) #返回与utc时间的时间差,以秒计算\
  6. # print(time.asctime()) #返回时间格式"Fri Aug 19 11:14:16 2016",
  7. # print(time.localtime()) #返回本地时间 的struct time对象格式
  8. # print(time.gmtime(time.time()-800000)) #返回utc时间的struc时间对象格式
  9.  
  10. # print(time.asctime(time.localtime())) #返回时间格式"Fri Aug 19 11:14:16 2016",
  11. #print(time.ctime()) #返回Fri Aug 19 12:38:29 2016 格式, 同上
  12.  
  13. # 日期字符串 转成 时间戳
  14. # string_2_struct = time.strptime("2016/05/22","%Y/%m/%d") #将 日期字符串 转成 struct时间对象格式
  15. # print(string_2_struct)
  16. # #
  17. # struct_2_stamp = time.mktime(string_2_struct) #将struct时间对象转成时间戳
  18. # print(struct_2_stamp)
  19.  
  20. #将时间戳转为字符串格式
  21. # print(time.gmtime(time.time()-86640)) #将utc时间戳转换成struct_time格式
  22. # print(time.strftime("%Y-%m-%d %H:%M:%S",time.gmtime()) ) #将utc struct_time格式转成指定的字符串格式
  23.  
  24. #时间加减
  25. import datetime
  26.  
  27. # print(datetime.datetime.now()) #返回 2016-08-19 12:47:03.941925
  28. #print(datetime.date.fromtimestamp(time.time()) ) # 时间戳直接转成日期格式 2016-08-19
  29. # print(datetime.datetime.now() )
  30. # print(datetime.datetime.now() + datetime.timedelta(3)) #当前时间+3天
  31. # print(datetime.datetime.now() + datetime.timedelta(-3)) #当前时间-3天
  32. # print(datetime.datetime.now() + datetime.timedelta(hours=3)) #当前时间+3小时
  33. # print(datetime.datetime.now() + datetime.timedelta(minutes=30)) #当前时间+30分
  34.  
  35. #
  36. # c_time = datetime.datetime.now()
  37. # print(c_time.replace(minute=3,hour=2)) #时间替换

对应格式:

  1. %a 本地(locale)简化星期名称
  2. %A 本地完整星期名称
  3. %b 本地简化月份名称
  4. %B 本地完整月份名称
  5. %c 本地相应的日期和时间表示
  6. %d 一个月中的第几天(01 - 31
  7. %H 一天中的第几个小时(24小时制,00 - 23
  8. %I 第几个小时(12小时制,01 - 12
  9. %j 一年中的第几天(001 - 366
  10. %m 月份(01 - 12
  11. %M 分钟数(00 - 59
  12. %p 本地am或者pm的相应符
  13. %S 秒(01 - 61
  14. %U 一年中的星期数。(00 - 53星期天是一个星期的开始。)第一个星期天之前的所有天数都放在第0周。
  15. %w 一个星期中的第几天(0 - 60是星期天)
  16. %W 和%U基本相同,不同的是%W以星期一为一个星期的开始。
  17. %x 本地相应日期
  18. %X 本地相应时间
  19. %y 去掉世纪的年份(00 - 99
  20. %Y 完整的年份
  21. %Z 时区的名字(如果不存在为空字符)
  22. %% ‘%’字符

格式对应

3.random模块

  1. #!/usr/bin/env python
  2. #_*_encoding: utf-8_*_
  3. import random
  4. print (random.random()) #0.6445010863311293
  5. #random.random()用于生成一个0到1的随机符点数: 0 <= n < 1.0
  6. print (random.randint(1,7)) #
  7. #random.randint()的函数原型为:random.randint(a, b),用于生成一个指定范围内的整数。
  8. # 其中参数a是下限,参数b是上限,生成的随机数n: a <= n <= b
  9. print (random.randrange(1,10)) #
  10. #random.randrange的函数原型为:random.randrange([start], stop[, step]),
  11. # 从指定范围内,按指定基数递增的集合中 获取一个随机数。如:random.randrange(10, 100, 2),
  12. # 结果相当于从[10, 12, 14, 16, ... 96, 98]序列中获取一个随机数。
  13. # random.randrange(10, 100, 2)在结果上与 random.choice(range(10, 100, 2) 等效。
  14. print(random.choice('liukuni')) #i
  15. #random.choice从序列中获取一个随机元素。
  16. # 其函数原型为:random.choice(sequence)。参数sequence表示一个有序类型。
  17. # 这里要说明一下:sequence在python不是一种特定的类型,而是泛指一系列的类型。
  18. # list, tuple, 字符串都属于sequence。有关sequence可以查看python手册数据模型这一章。
  19. # 下面是使用choice的一些例子:
  20. print(random.choice("学习Python"))#学
  21. print(random.choice(["JGood","is","a","handsome","boy"])) #List
  22. print(random.choice(("Tuple","List","Dict"))) #List
  23. print(random.sample([1,2,3,4,5],3)) #[1, 2, 5]
  24. #random.sample的函数原型为:random.sample(sequence, k),从指定序列中随机获取指定长度的片断。sample函数不会修改原有序列。

实际应用:

  1. #!/usr/bin/env python
  2. # encoding: utf-8
  3. import random
  4. import string
  5. #随机整数:
  6. print( random.randint(0,99)) #
  7.  
  8. #随机选取0到100间的偶数:
  9. print(random.randrange(0, 101, 2)) #
  10.  
  11. #随机浮点数:
  12. print( random.random()) #0.2746445568079129
  13. print(random.uniform(1, 10)) #9.887001463194844
  14.  
  15. #随机字符:
  16. print(random.choice('abcdefg&#%^*f')) #f
  17.  
  18. #多个字符中选取特定数量的字符:
  19. print(random.sample('abcdefghij',3)) #['f', 'h', 'd']
  20.  
  21. #随机选取字符串:
  22. print( random.choice ( ['apple', 'pear', 'peach', 'orange', 'lemon'] )) #apple
  23. #洗牌#
  24. items = [1,2,3,4,5,6,7]
  25. print(items) #[1, 2, 3, 4, 5, 6, 7]
  26. random.shuffle(items)
  27. print(items) #[1, 4, 7, 2, 5, 3, 6]

随机生成验证码:

  1. # _*_ coding:utf-8 _*_
  2. __author__ = "ZingP"
  3.  
  4. import random
  5. security_code = ''
  6. for i in range(0,5):
  7. j = random.randrange(0,5)
  8. if i == j:
  9. ptm = random.randint(0,9)
  10. else:
  11. ptm = chr(random.randint(97,122))
  12. security_code += str(ptm)
  13. print('security code:',security_code )

4.os

提供对操作系统进行调用的接口:

  1. os.path.basename() 去掉目录路径, 返回文件名
  2. os.path.dirname() 去掉文件名, 返回目录路径
  3. os.path.join() 将分离的各部分组合成一个路径名
  4. os.path.split() 返回目录和文件的元组 (dir, file)【给的是目录则会返回最后一个目录名和前面的路径】
  5. os.path.splitdrive() 返回盘符和路径的元组 (drivename, pathname)
  6. os.path.splitext() 返回文件名和扩展名的元组(filename, extension)
  7. os.path.abspath() 获得绝对路径(不太好使)
  8. os.path.normpath() 规范path字符串形式
  9.  
  10. os.path.getatime() 返回最近访问时间的时间戳
  11. os.path.getctime() 返回文件创建时间的时间戳
  12. os.path.getmtime() 返回最近文件修改时间的时间戳
  13. os.path.getsize() 返回文件或目录大小(以字节为单位)
  14. os.path.walk() 搜索目录下的所有文件
  15.  
  16. os.path.exists() 指定路径(文件或目录)是否存在
  17. os.path.isabs() 指定路径是否为绝对路径
  18. os.path.isdir() 指定路径是否存在且为一个目录
  19. os.path.isfile() 指定路径是否存在且为一个文件
  20. os.path.islink() 指定路径是否存在且为一个符号链接
  21. os.path.ismount() 指定路径是否存在且为一个挂载点
  22. os.path.samefile() 两个路径名是否指向同个文件
  23.  
  24. os.environ 获取系统环境变量
  25. os.linesep 获取本系统的换行符
  26. os.sep 获取路径分割符
  27. os.pathsep 路径与路径之间的分割符字符串
  28. os.curdir 当前工作目录的字符串名称(.)
  29. os.pardir (当前工作目录的)父目录字符串名称(..)
  30. os.name 输出字符串指示当前系统平台'nt'->win;'posix'->Linux
  31.  
  32. os.rename(old, new) 重命名
  33. os.remove() 删除文件
  34. os.listdir() 列出目录下的文件(目录也是文件):
  35. os.getcwd() 获取当前工作目录:
  36. os.chdir() 改变工作目录:
  37. os.makedirs() 创建多级目录
  38. os.mkdir() 创建单个目录
  39. os.removedirs() 删除所给路径最后一个目录下所有空目录。
  40. os.rmdir("test") 删除单个目录
  41. os.stat(file) 获取文件属性
  42. os.chmod(file) 修改文件权限与时间戳
  43. os.system("dir") 执行操作系统命令
  44. os.exec()/os.execvp()启动新进程
  45. os.spawnv() 在后台执行程序
  46. os.exit()/os._exit() 终止当前进程
  47. os.ismount() 是否挂载

5.sys

常用函数

  1. sys.argv # 命令行参数List,python t.py 1 sys.arsv[0]是t.py
  2. sys.exit(n) # 退出程序,正常退出时exit(0)
  3. sys.version # 获取Python解释程序的版本信息
  4. sys.maxint # 最大的Int值
  5. sys.path # 返回模块的搜索路径,初始化时使用PYTHONPATH环境变量的值
  6. sys.platform # 返回操作系统平台名称
  7. sys.getrecursionlimit() # 获取最大递归层数
  8. sys.setrecursionlimit(2000) # 设置最大递归层数为2000
  9. sys.stdout.write('please:')
  10. val = sys.stdin.readline()[:-1]
  11. sys.modules # 已加载模块的字典
  12. sys.version # 版本信息
  13. sys.version_info # 版本信息的命名元组
  14. sys.copyright # 获取python版权相关信息
  15. sys.builtin_module_names # 获得python内建模块名称(字符串元组)

示例:

  1. >>> import sys
  2. >>> sys.getrecursionlimit()
  3. 1000
  4. >>> sys.setrecursionlimit(2000)
  5. >>> sys.getrecursionlimit()
  6. 2000

6.shutil模块

更多:http://www.cnblogs.com/wupeiqi/articles/4963027.html

7.json & pickle模块

用于序列化的两个模块

  • json,用于字符串 和 python数据类型间进行转换

  • pickle,用于python特有的类型 和 python的数据类型间进行转换

Json模块提供了四个功能:dumps、dump、loads、load

pickle模块提供了四个功能:dumps、dump、loads、load

  1. import pickle
  2. a = {'a':'bc','c':'hello','d':123,'e':'world'}
  3. f = open('a.pkl','wb')
  4. pickle.dump(a,f)#如果跨语言平台使用json
  5. f.close()
  6. '''
  7. >>> a = {'a':'bc','c':'hello','d':123,'e':'world'}
  8. >>> import json
  9. >>> json.dumps(a)
  10. '{"a": "bc", "d": 123, "e": "world", "c": "hello"}'
  11. >>> b = json.dumps(a)
  12. >>> json.loads(b)
  13. {'a': 'bc', 'd': 123, 'e': 'world', 'c': 'hello'}
  14. '''
  1. import pickle
  2. f = open('a.pkl','rb')
  3. acc = pickle.load(f)
  4. print(acc)

8.shelve

9.xml处理
10.yaml处理
11.configparser

12.hashlib

用于加密相关的操作,代替了md5模块和sha模块,主要提供 SHA1, SHA224, SHA256, SHA384, SHA512 ,MD5 算法

  1. #! /usr/bin/env python3
  2.  
  3. import hashlib
  4.  
  5. # ######## md5 ########
  6. hash = hashlib.md5()
  7. hash.update(b'love you baby..')
  8. print(hash.hexdigest())
  9.  
  10. # ######## sha1 ########
  11. hash = hashlib.sha1()
  12. hash.update(b'love you baby..')
  13. print(hash.hexdigest())
  14.  
  15. # ######## sha256 ########
  16. hash = hashlib.sha256()
  17. hash.update(b'love you baby..')
  18. print(hash.hexdigest())
  19.  
  20. # ######## sha384 ########
  21. hash = hashlib.sha384()
  22. hash.update(b'love you baby..')
  23. print(hash.hexdigest())
  24.  
  25. # ######## sha512 ########
  26. hash = hashlib.sha512()
  27. hash.update(b'love you baby..')
  28. print(hash.hexdigest())

以上加密算法虽然依然非常厉害,但时候存在缺陷,即:通过撞库可以反解。所以,有必要对加密算法中添加自定义key再来做加密。

  1. #! /usr/bin/env python3
  2.  
  3. import hashlib
  4.  
  5. # ######## md5 ########
  6. hash = hashlib.md5(b'898oaFs09f')
  7. hash.update(b'love you baby...')
  8. print(hash.hexdigest())

还不够吊?python 还有一个 hmac 模块,它内部对我们创建 key 和 内容 再进行处理然后再加密

  1. import hmac
  2. h = hmac.new(b'zingp')
  3. h.update(b'hello')
  4. print(h.hexdigest())

13.subprocess

可以执行shell命令的相关模块和函数有:

  • os.system
  • os.spawn*
  • os.popen*          --废弃
  • popen2.*           --废弃
  • commands.*      --废弃,3.x中被移除

以上执行shell命令的相关的模块和函数的功能均在 subprocess 模块中实现,并提供了更丰富的功能。

call 

执行命令,返回状态码

  1. ret = subprocess.call(["ls", "-l"], shell=False)
  2. ret = subprocess.call("ls -l", shell=True)

shell = True ,允许 shell 命令是字符串形式

check_call

执行命令,如果执行状态码是 0 ,则返回0,否则抛异常

  1. subprocess.check_call(["ls", "-l"])
  2. subprocess.check_call("exit 1", shell=True)

check_output

执行命令,如果状态码是 0 ,则返回执行结果,否则抛异常

  1. subprocess.check_output(["echo", "Hello World!"])
  2. subprocess.check_output("exit 1", shell=True)

调用subprocess.run(...)是推荐的常用方法,在大多数情况下能满足需求。

  1. >>> subprocess.run(["ls", "-l"]) # doesn't capture output
  2. CompletedProcess(args=['ls', '-l'], returncode=0)
  3.  
  4. >>> subprocess.run("exit 1", shell=True, check=True)
  5. Traceback (most recent call last):
  6. ...
  7. subprocess.CalledProcessError: Command 'exit 1' returned non-zero exit status 1
  8.  
  9. >>> subprocess.run(["ls", "-l", "/dev/null"], stdout=subprocess.PIPE)
  10. CompletedProcess(args=['ls', '-l', '/dev/null'], returncode=0,
  11. stdout=b'crw-rw-rw- 1 root root 1, 3 Jan 23 16:23 /dev/null\n')

subprocess.Popen(...)

用于执行复杂的系统命令

参数:

    • args:shell命令,可以是字符串或者序列类型(如:list,元组)
    • bufsize:指定缓冲。0 无缓冲,1 行缓冲,其他 缓冲区大小,负值 系统缓冲
    • stdin, stdout, stderr:分别表示程序的标准输入、输出、错误句柄
    • preexec_fn:只在Unix平台下有效,用于指定一个可执行对象(callable object),它将在子进程运行之前被调用
    • close_sfs:在windows平台下,如果close_fds被设置为True,则新创建的子进程将不会继承父进程的输入、输出、错误管道。
      所以不能将close_fds设置为True同时重定向子进程的标准输入、输出与错误(stdin, stdout, stderr)。
    • shell:同上
    • cwd:用于设置子进程的当前目录
    • env:用于指定子进程的环境变量。如果env = None,子进程的环境变量将从父进程中继承。
    • universal_newlines:不同系统的换行符不同,True -> 同意使用 \n
    • startupinfo与createionflags只在windows下有效
      将被传递给底层的CreateProcess()函数,用于设置子进程的一些属性,如:主窗口的外观,进程的优先级等等
  1. import subprocess
  2. ret1 = subprocess.Popen(["mkdir","t1"])
  3. ret2 = subprocess.Popen("mkdir t2", shell=True)

终端输入的命令分为两种:

  • 输入即可得到输出,如:ifconfig
  • 输入进行某环境,依赖再输入,如:python
    1. import subprocess
    2.  
    3. obj = subprocess.Popen("mkdir t3", shell=True, cwd='/home/dev',)

示例:

  1. import subprocess
  2.  
  3. obj = subprocess.Popen(["python"], stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
  4. obj.stdin.write('print 1 \n ')
  5. obj.stdin.write('print 2 \n ')
  6. obj.stdin.write('print 3 \n ')
  7. obj.stdin.write('print 4 \n ')
  8. obj.stdin.close()
  9.  
  10. cmd_out = obj.stdout.read()
  11. obj.stdout.close()
  12. cmd_error = obj.stderr.read()
  13. obj.stderr.close()
  14.  
  15. print cmd_out
  16. print cmd_error
  1. import subprocess
  2.  
  3. obj = subprocess.Popen(["python"], stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
  4. obj.stdin.write('print 1 \n ')
  5. obj.stdin.write('print 2 \n ')
  6. obj.stdin.write('print 3 \n ')
  7. obj.stdin.write('print 4 \n ')
  8.  
  9. out_error_list = obj.communicate()
  10. print out_error_list
  1. import subprocess
  2.  
  3. obj = subprocess.Popen(["python"], stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
  4. out_error_list = obj.communicate('print "hello"')
  5. print out_error_list

14.logging模块

用于便捷记录日志且线程安全的模块

  1. #! /usr/bin/env python3
  2. import logging
  3.  
  4. logging.basicConfig(filename='mylog.log',
  5. format='%(asctime)s - %(name)s - %(levelname)s -%(module)s: %(message)s',
  6. datefmt='%Y-%m-%d %H:%M:%S %p',
  7. level=10)
  8. # level设置值就是当出现这个情况或比这个值更严重的情况才记录日志
  9. logging.debug('')
  10. logging.info('')
  11. logging.warning('')
  12. logging.error('')
  13. logging.critical('')
  14. logging.log(10, 'logqqq')

运行结果,mylog.log文件中的内容:

  1. 2016-11-22 10:52:37 AM - root - DEBUG -test1: 1111
  2. 2016-11-22 10:52:37 AM - root - INFO -test1: 2222
  3. 2016-11-22 10:52:37 AM - root - WARNING -test1: 3333
  4. 2016-11-22 10:52:37 AM - root - ERROR -test1: 4444
  5. 2016-11-22 10:52:37 AM - root - CRITICAL -test1: 5555
  6. 2016-11-22 10:52:37 AM - root - DEBUG -test1: logqqq

日志的几个等级:

  1. CRITICAL = 50
  2. FATAL = CRITICAL
  3. ERROR = 40
  4. WARNING = 30
  5. WARN = WARNING
  6. INFO = 20
  7. DEBUG = 10
  8. NOTSET = 0

日志的格式,可以配置下面的属性:

15.re正则表达式

常用正则符号

  1. '.' 默认匹配除\n之外的任意一个字符,若指定flag DOTALL,则匹配任意字符,包括换行
  2. '^' 匹配字符开头,若指定flags MULTILINE,这种也可以匹配上(r"^a","\nabc\neee",flags=re.MULTILINE)
  3. '$' 匹配字符结尾,或e.search("foo$","bfoo\nsdfsf",flags=re.MULTILINE).group()也可以
  4. '*' 匹配*号前的字符0次或多次,re.findall("ab*","cabb3abcbbac") 结果为['abb', 'ab', 'a']
  5. '+' 匹配前一个字符1次或多次,re.findall("ab+","ab+cd+abb+bba") 结果['ab', 'abb']
  6. '?' 匹配前一个字符1次或0
  7. '{m}' 匹配前一个字符m
  8. '{n,m}' 匹配前一个字符nm次,re.findall("ab{1,3}","abb abc abbcbbb") 结果'abb', 'ab', 'abb']
  9. '|' 匹配|左或|右的字符,re.search("abc|ABC","ABCBabcCD").group() 结果'ABC'
  10. '(...)' 分组匹配,re.search("(abc){2}a(123|456)c", "abcabca456c").group() 结果 abcabca456c
  11.  
  12. '\A' 只从字符开头匹配,re.search("\Aabc","alexabc") 是匹配不到的
  13. '\Z' 匹配字符结尾,同$
  14. '\d' 匹配数字0-9
  15. '\D' 匹配非数字
  16. '\w' 匹配[A-Za-z0-9]
  17. '\W' 匹配非[A-Za-z0-9]
  18. 's' 匹配空白字符、\t\n\r , re.search("\s+","ab\tc1\n3").group() 结果 '\t'
  19.  
  20. '(?P<name>...)' 分组匹配 re.search("(?P<province>[0-9]{4})(?P<city>[0-9]{2})(?P<birthday>[0-9]{4})","").groupdict("city") 结果{'province': '', 'city': '', 'birthday': ''}

示例:

  1. # _*_ coding:utf-8 _*_
  2. __author__ = "ZingP"
  3. import re
  4.  
  5. a = 'abc1234\(hh+12hhdd][wdbrgwnq'
  6. # '.'默认匹配除\n之外的任意一个字符,若指定flag DOTALL,则匹配任意字符,包括换行
  7. b = re.search('.', a)
  8. print(b.group()) # result:a
  9.  
  10. c = re.search('.', '\nbcd', flags=re.DOTALL)
  11. print(c) # result:<_sre.SRE_Match object; span=(0, 1), match='\n'>
  12. # 这里print(c.group()) 什么也打印不了,\n不能用group???
  13.  
  14. # '^'匹配字符开头,若指定flags MULTILINE,这种也可以匹配上(r"^a","\nabc\neee",flags=re.MULTILINE)
  15. print(re.search('^b', 'bard').group()) # result: b
  16.  
  17. # '$'匹配字符结尾,或e.search("foo$","bfoo\nsdfsf",flags=re.MULTILINE).group()也可以
  18. print(re.search('2$', 'ggcba12').group()) # result:2
  19.  
  20. # 匹配*号前的字符0次或多次
  21. print(re.search('ac*', 'accqwiuewac457ab').group()) # result:acc
  22. print(re.findall('ac*', 'accqwiuewac457ab')) # result:['acc', 'ac', 'a']
  23.  
  24. # '+' 匹配前一个字符1次或多次
  25. print(re.findall("ab+", "ab+cd+abb+bba")) # result:['ab', 'abb']
  26.  
  27. # '?' 匹配前一个字符1次或0次
  28. print(re.findall('ab?', 'cdab90oladh')) # result:['ab', 'a']
  29.  
  30. # '{m}' 匹配前一个字符m次
  31. print(re.findall('ab{3}', 'cdab90olabbbbcdh')) # result:['abbb']
  32.  
  33. # '{n,m}' 匹配前一个字符n到m次,re.findall("ab{1,3}","abb abc abbcbbb") 结果'abb', 'ab', 'abb']
  34. print(re.findall('ab{1,3}', 'cdab90olabbbbcdh')) # ['ab', 'abbb']
  35.  
  36. # '|' 匹配|左或|右的字符,re.search("abc|ABC","ABCBabcCD").group() 结果'ABC'
  37. print(re.findall("abc|ABC","ABCB23abcCD")) # ['ABC', 'abc']
  38.  
  39. # '(...)' 分组匹配, 结果 abcabca456c
  40. print(re.search("(abc){2}a(123|456)c", "abcabca456c").group()) # abcabca456c
  41.  
  42. # 关于反斜杠
  43. print(re.search(r'\\', '\d\c\b')) # <_sre.SRE_Match object; span=(0, 1), match='\\'>
  44. print(re.findall(r'\\', 'ss\d\c\b')) # ['\\', '\\']???
  45. print(re.search('\\\\', 'd\c\b').group()) # \
  46.  
  47. print(re.search("(?P<id>[0-9]+)(?P<name>[A-Za-z]+)", "as123alex+++").groupdict())
  48. # {'name': 'alex', 'id': '123'}
  49.  
  50. # re.sub匹配到的替换 count为替换次数
  51. print(re.sub('[0-9]+', '|', 'abc12df34hh56n', count=2)) # abc|df|hh56n
  52. print(re.sub('[0-9]+', '|', 'abc12df34hh56n', count=3)) # abc|df|hh|n
  53.  
  54. print(re.findall("[0-9]{1,3}","aa1xxx2y3456")) # ['1', '2', '345', '6']
  55. print(re.findall("[0-9]{1,3}","aa1xxx2y3456")) # ['1', '2', '345', '6']

语法:

  1. re.match 从头开始匹配
  2. re.search 匹配包含
  3. re.findall 把所有匹配到的字符放到以列表中的元素返回
  4. re.splitall 以匹配到的字符当做列表分隔符
  5. re.sub 匹配字符并替换

了解几个匹配模式:

  1. re.I(re.IGNORECASE): 忽略大小写(括号内是完整写法,下同)
  2. M(MULTILINE): 多行模式,改变'^''$'的行为(参见上图)
  3. S(DOTALL): 点任意匹配模式,改变'.'的行为

16.glob模块

python【第五篇】常用模块学习的更多相关文章

  1. Python 之路 Day5 - 常用模块学习

    本节大纲: 模块介绍 time &datetime模块 random os sys shutil json & picle shelve xml处理 yaml处理 configpars ...

  2. Python之路,Day5 - 常用模块学习 (转载Alex)

    本节大纲: 模块介绍 time &datetime模块 random os sys shutil json & picle shelve xml处理 yaml处理 configpars ...

  3. Day5 - Python基础5 常用模块学习

    Python 之路 Day5 - 常用模块学习   本节大纲: 模块介绍 time &datetime模块 random os sys shutil json & picle shel ...

  4. 第六章:Python基础の反射与常用模块解密

    本课主题 反射 Mapping 介绍和操作实战 模块介绍和操作实战 random 模块 time 和 datetime 模块 logging 模块 sys 模块 os 模块 hashlib 模块 re ...

  5. Python第五章__模块介绍,常用内置模块

    Python第五章__模块介绍,常用内置模块 欢迎加入Linux_Python学习群  群号:478616847 目录: 模块与导入介绍 包的介绍 time &datetime模块 rando ...

  6. 简学Python第五章__模块介绍,常用内置模块

    Python第五章__模块介绍,常用内置模块 欢迎加入Linux_Python学习群  群号:478616847 目录: 模块与导入介绍 包的介绍 time &datetime模块 rando ...

  7. Python之路,Day21 - 常用算法学习

    Python之路,Day21 - 常用算法学习   本节内容 算法定义 时间复杂度 空间复杂度 常用算法实例 1.算法定义 算法(Algorithm)是指解题方案的准确而完整的描述,是一系列解决问题的 ...

  8. Python【第五篇】模块、包、常用模块

    一.模块(Module) 在计算机程序的开发过程中,随着程序代码越写越多,在一个文件里代码就会越来越长,越来越不容易维护. 为了编写可维护的代码,我们把很多函数分组,分别放到不同的文件里,这样,每个文 ...

  9. Python常用模块学习

    1.模块介绍 2.time & datetime模块 3.random 4.os 5.sys 6.shutil 7.json&pickle 8.shelve 9.xml处理 10.ya ...

  10. Python基础5 常用模块学习

    本节大纲: 模块介绍 time &datetime模块 random os sys shutil json & picle shelve xml处理 yaml处理 configpars ...

随机推荐

  1. QT: QByteArray储存二进制数据(包括结构体,自定义QT对象)

      因为利用QByteArray可以很方便的利用其API对内存数据进行访问和修改, 构建数据库blob字段时必不可少; 那如何向blob内写入自定义的结构体和类 1. 利用memcpy拷贝内存数据 / ...

  2. C#_delegate - 用委托实现事件,Display和Log类都使用Clock对象

    //public event SecondChangeHandler OnSecondChange; 若将委托加上event,则视作是事件,不是委托,外围就不能直接对OnSecondChange传值 ...

  3. SQL Server 中添加用户

    在对象资源管理器中点击安全性,选择登录名-新建登录名

  4. LoadRunner测试问题

    1.关于Error -27791: Error -27790:Error -27740: 错误如下: Action.c(198): Error -27791: Server has shut down ...

  5. IIS7下w3wp.exe进程CPU100%问题解决办法

      IIS下经常会出现w3wp.exe进程的CPU使用率达到100%的情况,在IIS7出现之前,要想确定问题所在,可以通过WinDbg来调试分析,但整个过程对技术水平要求非常高,可以参考http:// ...

  6. 《Mysql 公司职员学习篇》 第二章 小A的惊喜

          第二章 小A的惊喜  ---- 认识数据库 吃完饭后,小Y和小A回到了家里,并打开电脑开始学习Mysql. 小Y:"小A,你平时的Excell文件很多的情况下,怎么样存放Exce ...

  7. Azure PowerShell (二)云服务

    .  浏览云服务Get-AzureService | Select-Object -Property ServiceName, Location,`@{Name='ProdIP';Expression ...

  8. 异常的应用finally与总结

    一.finally{   },finally块中放入一定要执行的代码,通常用来关闭数据库,关闭链接,节约资源,所以finally中的内容一定要被执行,但是有一张情况不被执行,就是catch(Excep ...

  9. 介绍map.entry接口

    Map是java中的接口,Map.Entry是Map的一个内部接口.java.util.Map.Entry接口主要就是在遍历map的时候用到. Map提供了一些常用方法,如keySet().entry ...

  10. Activti跳过中间节点的helloworld实例程序

    http://blog.csdn.net/songzheng_741/article/details/17289633 此实例是一个最简单的在运行时人为动态改变流程运转的实例,意在为任意流.驳回等功能 ...