Time模块 1 2 3 4 5 6 7 8 9 10 11 12 13 14 import time 1. time.time() 2. time.perf_counter()3. time.process_time()4. time.localtime()5. time.gmtiime()6. time.strftime('%Y--%m--%d %H--%M--%S' ,time.localtime())7. time.strptime('2019--06--11 08--30--46' ,'%Y--%m--%d %H--%M--%S' )print(a) print(a.tm_year) print(a.tm_wday) 8. time.ctime(1 ) 9. time.mktime()
Datatime模块 1 2 3 import datetimeprint(datetime.datetime.now()) 2019 -06 -11 13 :30 :10.367575
Ramdom模块 1 2 3 4 5 6 7 8 9 10 11 12 13 14 import random1. random.random() print(random.random()) print(random.random()) print(random.random()) print(random.random()) 2. random.randint(1 ,8 ) 3. random.choic('hello' ) print(random.choice('hello' )) print(random.choice(['123' ,4 ,[1 ,2 ]])) 4. random.sample(['123' ,4 ,[1 ,2 ]],2 ) print(random.sample(['123' ,4 ,[1 ,2 ]],2 )) 5. random.randrange(1 ,3 )
random模块生成随机验证码
1 2 3 4 5 6 7 def c_void () : viod='' for i in range(5 ): add=random.choice([random.randrange(10 ),chr(random.randint(65 ,90 ))]) viod += str(add) print(viod) c_viod()
Sys模块 1.sys.argv() #传入参数 可以与python解释器交互
1 2 3 4 5 6 def down () : pass if sys.argv[1 ]=='post' : print("down" ) elif sys.argv[2 ]=='path' : print('up' )
2.sys.exit(0) #退出 0正常
3.sys.path #查找模块路径
4.sys.platform ##输出操纵系统类型
Hashlib 加密模块 1 2 3 4 5 6 7 8 a=hashlib.md5() a.update('abc' .encode('utf-8' )) print(a.hexdigest()) a.update('chen' .encode('utf-8' )) print(a.hexdigest()) b=hashlib.md5() b.update('abcchen' .encode('utf-8' )) print(b.hexdigest())
Os模块 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 os.getcwd() os.chdir('' dirname) os.curdir() os.pardir() os.makedirs('dirname1/dirname2' ) os.removedirs('dirname1' ) os.mkdir('dirname' ) os.rmdir('dirname' ) os.listdir('dirname' ) os.remove() os.rename("oldname" ,"newname" ) os.stat('path/filename' ) print(os.stat(r"E:/full-stack/week1/day5" )) os.sep os.linesep os.pathsep os.name os.system("bash command" ) os.environ os.path.abspath(path) os.path.split(path) os.path.dirname(path) os.path.basename(path) print(os.path.abspath('E:/full-stack/week1/day5' )) print(os.path.dirname('E:/full-stack/week1/day5' )) print(os.path.basename('E:/full-stack/week1/day5' )) os.path.exists(path)
Logging 日志模块 logging.debug(‘debug message’)
logging.info(‘info message’)
logging.warning(‘hello everone’)
logging.error(‘error message’)
logging.critical(‘critical message’)
默认输出级别为warning C>E>W>I>D
自定义日志存储
1 2 3 4 5 6 7 8 9 10 11 logging.basicConfig(level=logging.DEBUG, format='%(asctime)s %(filename)s[line:%(lineno)d] %(levelname)s %(message)s' , datefmt='%a, %d %b %Y %H:%M:%S' , filename='test.log' , filemode='a' ) logging.debug('debug message5' ) logging.info('info message2' ) logging.warning('warning message12' ) logging.error('error message8' ) logging.critical('critical message' )
创建对象调用日志,输入到文件和屏幕
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 logger = logging.getLogger() logger.setLevel(logging.DEBUG) fh = logging.FileHandler('test.log' ) ch = logging.StreamHandler() formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s' ) fh.setFormatter(formatter) ch.setFormatter(formatter) logger.addHandler(fh) logger.addHandler(ch) logger.debug('logger debug message' ) logger.info('logger info message' ) logger.warning('logger warning message' ) logger.error('logger error message' ) logger.critical('logger critical message' )
Re——正则 正则是对字符串进行处理的,引入正则的目的是为了进行模糊匹配。
正则的元字符 . 通配符 不能代指\n 只能代指一个字符
^ 尖角符 只在开始匹配
$ 只在结尾匹配
重复匹配 * [0,+00) + [1,+00) ? [0,1] {} 可定义匹配次数
结论 * = {0,+00} + = {1,+00} ? ={0,1}
特殊字符 [] 字符集 取消元字符的特殊功能 (\ ^ -)例外
[ ^ ] 取反
\
# 1.反斜杠后面跟元字符去除其特殊功能
# 2.反斜杠后面跟普通字符实现特殊功能
# \d 匹配任何十进制数 相当于类[0-9]
# \D 匹配任何非数字字符 [^0-9]
# \s 匹配任何空白字符 相当于类[\t \n \r \f \v]
# \S 匹配任何空白字符 相当于类[^\t \n \r \f \v]
# \w 匹配任何字符数字字符 相当于类[1-9a-zA-Z]
# \W 匹配任何非字符数字字符 相当于类[^1-9a-zA-Z]
# \b 匹配一个特殊字符边界 也就是指单词和特殊字符的位置
() 分组
使用举例 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 a=re.findall('w..l' ,'hello world' ) print(a) b=re.findall('w..l' ,'hello w\nld' ) print(b) a=re.findall('^h...o' ,'hello world hoxlo' ) print(a) b=re.findall('^h...o' ,'hlllll hello' ) print(b) a=re.findall('a..x$' ,'asdsafasalex' ) print(a) b=re.findall('a..x$' ,'asdsafasalexx' ) print(b) a=re.findall('ab*' ,'asfasdabbbabs' ) print(a) a=re.findall('a+b' ,'asfasdabbbabs' ) print(a) a=re.findall('a?b' ,'asfasdabbbabs' ) print(a) a=re.findall('a{1,3}b' ,'asfasdaaaaaabbbabs' ) print(a) a=re.findall('a[a-z]x' ,'adxabxaex' ) b=re.findall('a[w,,]x' ,'adxa*xa,x' ) print(a,'\n' ,b) a=re.findall('[1-9,a-z,A-Z]' ,'adxabxaex456as4f65as489aw4d6' ) print(a) a=re.findall('[^4,5]' ,'466865416132132' ) print(a) print(re.findall(r'i\b' ,'hello,i am a list' )) a=re.search('sb' ,'asfasbffsdsb' ) print(re.search('sb' ,'asfasbffsdsb' )) print(a.group()) a=re.search('a\+' ,'a+hi' ).group() print(a) print(re.search('(as)+' ,'dsasdasfgdgdasas' ).group()) print(re.search('(as)|3' ,'3as3' ).group()) a=re.search('(?P<id>\d{3})/(?P<name>\w{3})' ,'weeew34ttt123/000' ) print(a.group()) print(a.group('id' )) print(a.group('name' ))
正则表达式的使用 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 正则表达式的方法: 1. re.findall() 2. re.search() 3. match() 4. re.split()'ads' .split('s' )a=re.split('[k,s]' ,'asjksdasfa' ) print(a) 5. re.sub()a=re.sub('a..x' ,'s..b' ,'gsdfasgsalexagdafas' ) print(a) re.findall('\.com' ,'asdas.comsda' ) obj=re.compile('\.com' )
Author:
FullSky
Permalink:
http://FullSky`★.com/2019/08/27/python模块1/
License:
Copyright (c) 2019 CC-BY-NC-4.0 LICENSE
Slogan:
能通途偶遇在这星球上,我多么够运。