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