Python基础(正则、序列化、常用模块和面向对象)
写在前面
天地不仁,以万物为刍狗;
一、正则
- 正则就是用一些具有特殊含义的符号组合到一起(称为正则表达式)来描述字符或者字符串的方法;
- 在线正则工具:http://tool.oschina.net/regex/
- 常用的元字符:
- 先来个匹配邮箱的小例子:
1 import re 2 s=''' 3 http://www.baidu.com 4 1011010101 5 egon@oldboyedu.com 6 你好 7 21213 8 010-3141 9 egon@163.com 10 ''' 11 # 注意:这个匹配规则可以在 http://tool.oschina.net/regex/ 这里拿到 ^_^ 12 patten_email = r"[\w!#$%&'*+/=?^_`{|}~-]+(?:\.[\w!#$%&'*+/=?^_`{|}~-]+)*@(?:[\w](?:[\w-]*[\w])?\.)+[\w](?:[\w-]*[\w])?" 13 res = re.findall(patten_email,s) 14 print(res) 15 16 --- 17 ['egon@oldboyedu.com', 'egon@163.com']
- re 模块
- 更多参考:http://www.cnblogs.com/linhaifeng/articles/6384466.html#_label13
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 This module provides regular expression matching operations similar to 2 those found in Perl. It supports both 8-bit and Unicode strings; both 3 the pattern and the strings being processed can contain null bytes and 4 characters outside the US ASCII range. 5 6 Regular expressions can contain both special and ordinary characters. 7 Most ordinary characters, like "A", "a", or "0", are the simplest 8 regular expressions; they simply match themselves. You can 9 concatenate ordinary characters, so last matches the string 'last'.
- re 提供的方法
# re 模块包含的方法如下:
1 This module exports the following functions: 2 match Match a regular expression pattern to the beginning of a string. 3 fullmatch Match a regular expression pattern to all of a string. 4 search Search a string for the presence of a pattern. 5 sub Substitute occurrences of a pattern found in a string. 6 subn Same as sub, but also return the number of substitutions made. 7 split Split a string by the occurrences of a pattern. 8 findall Find all occurrences of a pattern in a string. 9 finditer Return an iterator yielding a match object for each match. 10 compile Compile a pattern into a RegexObject. 11 purge Clear the regular expression cache. 12 escape Backslash all non-alphanumerics in a string.
- re.findall()
- Find all occurrences of a pattern in a string.
- 示例1:基础元字符的匹配
1 import re 2 3 print(re.findall('\W','as213df_*|')) 4 print(re.findall('a\wb','a_b a3b aEb a*b')) 5 6 print(re.findall('\n','a123\nbcdef')) 7 print(re.findall('\t','a123\tbc\td\tef')) 8 9 print(re.findall('a.c','abc a1c a*c a|c abd aed ac')) 10 print(re.findall('a.c','abc a1c a*c a|c abd aed a\nc')) # . 不能匹配 换行符 11 print(re.findall('a.c','abc a1c a*c a|c abd aed a\nc',re.S)) # 让 . 能够匹配到换行符 12 13 print(re.findall('a[1,2\n]c','a2c a,c abc a1c a*c a|c abd aed a\nc')) 14 print(re.findall('a[0-9]c','a2c a,c abc a1c a*c a|c abd aed a\nc')) 15 print(re.findall('a[-0-9a-zA-Z*]c','a1c abc a*c a-c aEc')) # []里的 - 必须放末尾或者开头 16 print(re.findall('a[0-9a-zA-Z*-]c','a1c abc a*c a-c aEc')) # []里的 - 必须放末尾或者开头 17 18 print(re.findall('a[^0-9]c','a1c abc a*c a-c aEc')) # [^0-9] 匹配非数字 19 20 --- 21 ['*', '|'] 22 ['a_b', 'a3b', 'aEb'] 23 ['\n'] 24 ['\t', '\t', '\t'] 25 ['abc', 'a1c', 'a*c', 'a|c'] 26 ['abc', 'a1c', 'a*c', 'a|c'] 27 ['abc', 'a1c', 'a*c', 'a|c', 'a\nc'] 28 ['a2c', 'a,c', 'a1c', 'a\nc'] 29 ['a2c', 'a1c'] 30 ['a1c', 'abc', 'a*c', 'a-c', 'aEc'] 31 ['a1c', 'abc', 'a*c', 'a-c', 'aEc'] 32 ['abc', 'a*c', 'a-c', 'aEc']
- 示例2:表示重复的几种情况: . | * | .* | .*? | + | ? | {n,m} | {n,} | {,m}
1 # . 匹配单个字符 2 print(re.findall('a.b','a1b')) 3 print(re.findall('a.b','a\nb')) # 没有匹配到换行符 4 print(re.findall('a.b','a\nb',re.S)) # 匹配换行符 5 print(re.findall('a.b','a\nb',re.DOTALL)) 6 7 --- 8 ['a1b'] 9 [] 10 ['a\nb'] 11 ['a\nb']
1 # * 匹配0个或多个前面的字符 2 print(re.findall('ab*','bbbbbbb')) 3 print(re.findall('ab*','a')) 4 print(re.findall('ab*','abbbb')) 5 6 --- 7 [] 8 ['a'] 9 ['abbbb']
1 # ? 匹配0个或者1个前面的字符 2 print(re.findall('ab?','a')) 3 print(re.findall('ab?','abbb')) 4 5 --- 6 ['a'] 7 ['ab']
# + 匹配1个或多个前面的字符 print(re.findall('ab+','a')) print(re.findall('ab+','ab')) print(re.findall('ab+','abbbbb')) --- [] ['ab'] ['abbbbb']
1 # {n,m} 指定匹配字符的个数 2 print(re.findall('ab{2}','abbbbb')) 3 print(re.findall('ab{2,4}','abbbbb')) 4 print(re.findall('ab{0,}','abbbbb')) 5 print(re.findall('ab{,3}','abbbbb')) 6 7 --- 8 ['abb'] 9 ['abbbb'] 10 ['abbbbb'] 11 ['abbb']
- 示例3: ?: 的使用
1 import re 2 3 print(re.findall('compan(y|ies)','Too many companies have gone bankrupt, and the next one is my company')) 4 print(re.findall('compan(?:y|ies)','Too many companies have gone bankrupt, and the next one is my company')) 5 # '|' 特性取值,'|' --> 或 6 print(re.findall('company|companies','Too many companies have gone bankrupt, and the next one is my company')) 7 print(re.findall('company|companies','My company have gone bankrupt, and many companies will bankrupt')) 8 9 --- 10 ['ies', 'y'] 11 ['companies', 'company'] 12 ['companies', 'company'] 13 ['company', 'companies']
- 示例4:反斜杠的困扰
1 import re 2 print(re.findall('a\\c','a\c')) # 匹配失败 3 print(re.findall('a\\\\c','a\c')) 4 # python解释器先解析一层,然后再交给re模块处理 5 # a\\\\c --Python解释器--> a\\c --re 模块-->re接到的是2个 '\', 最终匹配到 'a\c' 6 print(re.findall(r'a\\c','a\c')) 7 # r 代表rawstring 即原生字符串,去除转意; 8 9 --- 10 [] 11 ['a\\c'] 12 ['a\\c']
- 示例5:贪婪匹配(.*)和非贪婪匹配(.*?)
1 import re 2 print(re.findall('a.*b','yqwasds#$dw2312ww-+as90b')) # 贪婪匹配 3 print(re.findall('a.*?b','yqwasds#$bw2312ww-+As90b')) # 非贪婪匹配 4 5 --- 6 ['asds#$dw2312ww-+as90b'] 7 ['asds#$b']
- 示例6:^ 和 $ 匹配开头和结尾
1 import re 2 print(re.findall('e','standby make love')) 3 print(re.findall('^e','egon make love')) 4 print(re.findall('^e','qegon make love')) 5 print(re.findall('e$','standby make love')) 6 7 --- 8 ['e', 'e'] 9 ['e'] 10 [] 11 ['e']
- 示例7:整数、小数的匹配
1 import re 2 # 找出所有数字 3 print(re.findall(r'-?\d+\.?\d*',"1-12*(60.2+(-40.35/5)-(-4.97*3))")) 4 # 找出所有非整数,即含有小数的 5 print(re.findall(r'-?\d+\.\d+',"1-12*(60.2+(-40.35/5)-(-4.97*3))")) 6 # 根据 '|' 特性,过滤出所有整数 7 print(re.findall(r'-?\d+\.\d+|(-?\d+)',"1-12*(60.2+(-40.35/5)-(-4.97*3))")) 8 9 --- 10 ['1', '-12', '60.2', '-40.35', '5', '-4.97', '3'] 11 ['60.2', '-40.35', '-4.97'] 12 ['1', '-12', '', '', '5', '', '3']
- 示例8:() 分组的使用
1 #():分组 2 print(re.findall('ab+','ababab123')) #['ab', 'ab', 'ab'] 3 # ['ab'],匹配到末尾的ab123中的ab 4 print(re.findall('(ab)+123','ababab123')) 5 # findall的结果不是匹配的全部内容,而是组内的内容, ?: 可以让结果为匹配的全部内容 6 print(re.findall('(?:ab)+123','ababab123')) 7 8 --- 9 ['ab', 'ab', 'ab'] 10 ['ab'] 11 ['ababab123']
- re.search()
- Search a string for the presence of a pattern.
1 import re 2 3 print(re.findall('e','alex make love')) 4 print(re.search('e','alex make love')) # 找到第一个匹配,然后返回一个包含该匹配信息的对象,该对象可以通过调用 group() 方法得到匹配的字符串; 5 print(re.search('e','make love')) 6 print(re.search('e','alex make love').group()) 7 print(re.search('^e','alex make love')) # 判断第一个字符是否是 e 8 9 --- 10 ['e', 'e', 'e'] 11 <_sre.SRE_Match object; span=(2, 3), match='e'> 12 <_sre.SRE_Match object; span=(3, 4), match='e'> 13 e 14 None
- re.match() , 同search,不过在字符串开始处进行匹配,完全可以用 search+^ 代替match;
- Match a regular expression pattern to the beginning of a string.
1 import re 2 3 print(re.match('e','alex make love')) 4 print(re.match('a','alex make love')) 5 print(re.search('^a','alex make love')) 6 print(re.match('a','alex make love').group()) 7 8 --- 9 None 10 <_sre.SRE_Match object; span=(0, 1), match='a'> 11 <_sre.SRE_Match object; span=(0, 1), match='a'> 12 a
- 扩展示例:
1 import re 2 3 print(re.search('al(e)x\smak(e)','alex make love').group()) 4 print(re.findall('al(e)x\smak(e)','alex make love')) 5 print(re.findall('al(?:e)x\smak(?:e)','alex make love')) 6 7 --- 8 alex make 9 [('e', 'e')] 10 ['alex make']
- re.fullmatch() ,完全匹配;
- Match a regular expression pattern to all of a string.
1 import re 2 3 print(re.fullmatch('e','ee')) 4 print(re.fullmatch('ee','ee')) 5 print(re.fullmatch('ee','ee').group()) 6 7 --- 8 None 9 <_sre.SRE_Match object; span=(0, 2), match='ee'> 10 ee
- re.sub()
- Substitute occurrences of a pattern found in a string.
- 示例1:
1 import re 2 3 print(re.sub('^a','A','alex make love a girl')) # 替换开头的a为A 4 print(re.sub('a','A','alex make love a girl')) 5 print(re.sub('a','A','alex make love a girl',1)) # 指定要替换几个,不指定就你全部替换; 6 print(re.sub('a','A','alex make love a girl',2)) 7 8 --- 9 Alex make love a girl 10 Alex mAke love A girl 11 Alex make love a girl 12 Alex mAke love a girl
-示例2:结合分组:() 实现位置调换
1 import re 2 print(re.sub('^(\w+)(\s)(\w+)(\s)(\w+)$',r'\5\2\3\4\1','alex make love')) 3 print(re.sub('^(\w+)(\s+)(\w+)(\s+)(\w+)$',r'\5_\3_\1','alex make love')) 4 print(re.sub('^(\w+)(\s+)(\w+)(\s+)(\w+)$',r'\5\2\3\4\1','alex make love')) 5 print(re.sub('^(\w+)(\W+)(\w+)(\W+)(\w+)$',r'\5 \3 \1','alex " \ + = make ---//== love')) 6 7 --- 8 love make alex 9 love_make_alex 10 love make alex 11 love make alex
- re.subn() , 同 sub() ,另外结果带有总共替换的个数;
- Same as sub, but also return the number of substitutions made.
1 import re 2 3 print(re.subn('a','A','alex make love a girl')) 4 print(re.subn('a','A','alex make love a girl',1)) 5 print(re.subn('a','A','alex make love a girl',2)) 6 7 --- 8 ('Alex mAke love A girl', 3) 9 ('Alex make love a girl', 1) 10 ('Alex mAke love a girl', 2)
- re.split()
- Split a string by the occurrences of a pattern.
1 import re 2 3 print(re.split('[ab]','abcd')) 4 print(re.split('[0-9]','one1two2three3four4five')) 5 6 --- 7 ['', '', 'cd'] # 先按'a'分割得到''和'bcd',再对''和'bcd'分别按'b'分割
8 ['one', 'two', 'three', 'four', 'five']
- re.finditer()
- Return an iterator yielding a match object for each match.
1 import re 2 3 content = "one1two2three3four4five" 4 res = re.finditer('[a-z]{3,5}', content) 5 print(type(res),res) 6 print(next(res)) 7 print(next(res)) 8 print(next(res)) 9 print(next(res).group()) 10 print(next(res).group()) 11 12 --- 13 <class 'callable_iterator'> <callable_iterator object at 0x00000000010F0048> 14 <_sre.SRE_Match object; span=(0, 3), match='one'> 15 <_sre.SRE_Match object; span=(4, 7), match='two'> 16 <_sre.SRE_Match object; span=(8, 13), match='three'> 17 four 18 five
- re.compile()
- Compile a pattern into a RegexObject.
1 import re 2 3 content = "one1two2three3four4five" 4 obj = re.compile(r'\D+') 5 res = obj.match(content) 6 print(type(res),res) 7 print(res.group()) 8 9 --- 10 <class '_sre.SRE_Match'> <_sre.SRE_Match object; span=(0, 3), match='one'> 11 one
- re.purge()
- Clear the regular expression cache.
- 清空缓存中的正则表达式;
- re.escape()
- Backslash all non-alphanumerics in a string.
- 返回一个字符串, 其中的所有非字母数字下划线字符都带有反斜杠;
1 import re 2 3 res = re.escape('www.pytho$n_*.o-rg') 4 print(type(res),res) 5 6 print(re.findall('a\\c','a\c')) 7 print(re.findall('a\\\\c','a\c')) 8 print(re.findall(r'a\\c','a\c')) 9 print(re.findall(re.escape('a\\c'),'a\c') 10 11 --- 12 <class 'str'> www\.pytho\$n_\*\.o\-rg 13 [] 14 ['a\\c'] 15 ['a\\c'] 16 ['a\\c']
- 应用:Python正则表达式中元字符的转义处理
二、序列化
- 序列化的概念
我们把对象(变量)从内存中变成可存储或传输的过程称之为序列化
之前我们学习过用eval内置方法可以将一个字符串转成python对象,不过,eval方法是有局限性的,对于普通的数据类型,json.loads和eval都能用,但遇到特殊类型的时候,eval就不管用了;
- eval
- 将字符串str当成有效的表达式来求值并返回计算结果;
1 a = "{'k1':'v1', 'k2':'v2'}" 2 print(type(a),a) 3 b = eval(a) 4 print(type(b),b,b.get('k2')) 5 6 --- 7 <class 'str'> {'k1':'v1', 'k2':'v2'} 8 <class 'dict'> {'k2': 'v2', 'k1': 'v1'} v2
- json
- josn.dump()
1 import json 2 dic = { 3 'name':'tian', 4 'age':25, 5 'job':'singer' 6 } 7 with open(r'json/dic2.json',mode='w',encoding='utf-8') as f: 8 res = json.dump(dic,f) 9 10 print(type(res),res) 11 12 --- 13 <class 'NoneType'> None
- json.load()
1 import json 2 with open(r'json/dic2.json',mode='r',encoding='utf-8') as f: 3 res = json.load(f) 4 5 print(type(res),res) 6 7 --- 8 <class 'dict'> {'age': 25, 'job': 'singer', 'name': 'tian'}
- json.dumps()
1 import json 2 dic = { 3 'name':'tian', 4 'age':25, 5 'job':'singer' 6 } 7 8 res = json.dumps(dic) 9 print(type(res),res) 10 with open('json/dic1.json','w') as f: 11 f.write(res)
- json.loads()
1 import json 2 3 with open(r'json/dic1.json',mode='r',encoding='utf-8') as f: 4 data = f.read() 5 res = json.loads(data) 6 print(type(res),res) 7 8 --- 9 <class 'dict'> {'age': 25, 'name': 'tian', 'job': 'singer'}
- pickle
三、常用模块介绍
- time
- random
- 生成随机字符;
1 # 生成随机验证码(数字、大小写字母),可以指定位数; 2 import random 3 def v_code(n=6): 4 res = '' 5 for i in range(n): 6 num = random.randint(0,9) 7 char_upper = chr(random.randint(65,90)) 8 char_lower = chr(random.randint(97,122)) 9 add = random.choice([num,char_upper,char_lower]) 10 res += str(add) 11 return res 12 13 res = v_code(10) 14 print(res) 15 16 --- 17 VQ6Yyu969G
- os
- os.path.abspath()
- os.path.abspath(__file__)
- os.path.basename()
- os.path.dirname()
- os.path.join()
- os.path.normpath()
- os.cpu_count()
- ...
1 import os 2 print(__name__) 3 print(__file__) 4 print(os.path.abspath(__file__)) 5 print('===第一种方式:获取上级目录的绝对路径===') 6 print(os.path.basename(os.path.abspath(__file__))) 7 print(os.path.dirname(os.path.abspath(__file__))) 8 print(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) 9 print('===第二种方式:获取上级目录的绝对路径===') 10 print(os.path.join( 11 os.path.abspath(__file__), 12 os.pardir, 13 os.pardir)) 14 print(os.path.normpath(os.path.join( 15 os.path.abspath(__file__), 16 os.pardir, 17 os.pardir))) 18 19 --- 20 __main__ 21 D:/soft/work/Python_17/day06/exercise/e1.py 22 D:\soft\work\Python_17\day06\exercise\e1.py 23 ===第一种方式:获取上级目录的绝对路径=== 24 e1.py 25 D:\soft\work\Python_17\day06\exercise 26 D:\soft\work\Python_17\day06 27 ===第二种方式:获取上级目录的绝对路径=== 28 D:\soft\work\Python_17\day06\exercise\e1.py\..\.. 29 D:\soft\work\Python_17\day06
- sys
- sys.path
- shutil
- shelve
- xml
- configparser
- hashlib
- subprocess
- logging
四、面向对象
-
五、练习
要求:
- 模拟实现一个ATM + 购物商城程序
1 额度 15000或自定义 2 实现购物商城,买东西加入 购物车,调用信用卡接口结账 3 可以提现,手续费5% 4 支持多账户登录 5 支持账户间转账 6 记录每月日常消费流水 7 提供还款接口 8 ATM记录操作日志 9 提供管理接口,包括添加账户、用户额度,冻结账户等。。。 10 用户认证用装饰器
功能结构如下:
代码实现:
1 CREDIT_SHOPPING 2 │ readme.txt 3 │ __init__.py 4 │ 5 ├─bin 6 │ admin.py 7 │ customer.py 8 │ __init__.py 9 │ 10 ├─conf 11 │ settings.py 12 │ warnning.py 13 │ __init__.py 14 │ 15 ├─core 16 │ credit.py 17 │ db_handler.py 18 │ logger.py 19 │ login.py 20 │ main.py 21 │ shopping.py 22 │ __init__.py 23 │ 24 ├─db 25 │ │ account_init.py 26 │ │ __init__.py 27 │ │ 28 │ └─accounts 29 ├─log 30 │ atm.log 31 │ credit.log 32 │ shopping.log 33 │ __init__.py 34 │ 35 └─user 36 manage.py 37 __init__.py
出处:http://www.cnblogs.com/standby/
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。