Python基础(正则、序列化、常用模块和面向对象)-day06
写在前面
天地不仁,以万物为刍狗;
一、正则
- 正则就是用一些具有特殊含义的符号组合到一起(称为正则表达式)来描述字符或者字符串的方法;
- 在线正则工具: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
Python基础(正则、序列化、常用模块和面向对象)-day06的更多相关文章
- 第六章:Python基础の反射与常用模块解密
本课主题 反射 Mapping 介绍和操作实战 模块介绍和操作实战 random 模块 time 和 datetime 模块 logging 模块 sys 模块 os 模块 hashlib 模块 re ...
- python自动化开发-[第六天]-常用模块、面向对象
今日概要: 1.常用模块 - os模块 - random模块 - shutil模块 - hashlib模块 - pickle/json模块 - shelve模块 - configparser模块 - ...
- Python基础学习之常用模块
1. 模块 告诉解释器到哪里查找模块的位置:比如sys.path.append('C:/python') 导入模块时:其所在目录中除源代码文件外,还新建了一个名为__pycache__ 的子目录,这个 ...
- Day07:常用模块,面向对象编程(对象&类)及内置函数
今日内容:1.常用模块2.面向对象编程(*****) 介绍面向对象编程 类 对象3.内置函数------------------------------1.面向过程编程 核心“ ...
- python基础--常用模块与面向对象基础
1常用模块 1.1 xml xml是实现不同语言或程序之间进行数据交换的协议 xml的格式如下: <?xml version="1.0"?> <data> ...
- python常用模块及面向对象(一)
目录: 常用模块之time模块 常用模块之random模块 常用模块之os模块 常用模块之sys模块 常用模块之subprocess模块 常用模块之json模块 常用模块之pickle模块 常用模块之 ...
- python面向编程: 常用模块补充与面向对象
一.常用模块 1.模块 的用用法 模块的相互导入 绝对导入 从sys.path (项目根目录)开始的完整路径 相对导入 是指相对于当前正在执行的文件开始的路径 只能用于包内模块相互间导入 不能超过顶层 ...
- Python学习—基础篇之常用模块
常用模块 模块,用一砣代码实现了某个功能的代码集合. 类似于函数式编程和面向过程编程,函数式编程则完成一个功能,其他代码用来调用即可,提供了代码的重用性和代码间的耦合.而对于一个复杂的功能来,可能需要 ...
- python(30)- 常用模块
模块就是py文件.python中能开辟作用域的只有函数.类和模块. for循环不能开辟作用域,for循环内的变量为全局变量.if...else...同for循环一样. 一 time模块 时间表示形式 ...
随机推荐
- Xshell 使用数字小键盘进行vim 写入操作.
Copy From http://blog.csdn.net/shenzhen206/article/details/51200869 感谢原作者 在putty或xshell上用vi/vim的时候,开 ...
- fswatch rsync配置使用 rsync 传输大量细小文件
贴一个脚本这个脚本是rsync远程同步使用的一个脚本 rsync -avz --progress --exclude .git /Users/piperck/Desktop/gogogo/x pipe ...
- python之函数(可选参数和混合参数)
代码举例: # 函数可选参数举例,hoppy参数可传可不传 def getinfo(name, age, hoppy=''): if hoppy: print("name:", n ...
- PythonProject(1)vim的Hustoj插件
打算写一个vim的插件,或者emacs的插件.可以在编辑器里打比赛,看rank,交代码.总之相当于一个桌面版的hustoj 这是上学期就有的一个脑洞产物,昨天学了Python的爬虫,发现这个东西很有实 ...
- MT【11】对数放缩题
解答:C 评论:这里讲几个背景知识
- ANDROID content provide 使用实例
Content Provider提供了一种多应用间数据共享的方式,比如:联系人信息可以被多个应用程序访问.Content Provider是个实现了一组用于提供其他应用程序存取数据的标准方法的类. 下 ...
- P1856 矩形周长
哇!这小破题坑了我好久. 扫描线+线段树 这题数据范围小,没离散化.真要离散化我还搞不好呢. 具体的看这个博客吧. 主要是这个坑爹的c,len把我搞了,其他的还好. 代码: #include < ...
- Python 爬虫入门(四)—— 验证码上篇(主要讲述验证码验证流程,不含破解验证码)
本篇主要讲述验证码的验证流程,包括如何验证码的实现.如何获取验证码.识别验证码(这篇是人来识别,机器识别放在下篇).发送验证码.同样以一个例子来说明.目标网址 http://icp.alexa.cn/ ...
- Maven的配置以及Eclipse的设置
配置maven仓库 先找到我们解压的maven的conf目录里面的setting.xml 然后加入我们本地仓库的位置(这里仓库所在文件夹是自定义的,比如:我把它放在了D盘根目录的一个文件夹) ecli ...
- 邮件发送-》http://service.mail.qq.com/cgi-bin/help?subtype=1&&id=28&&no=1001256
昨天使用发送邮件报了一个错 http://service.mail.qq.com/cgi-bin/help?subtype=1&&id=28&&no=1001256 主 ...