一鼓作气 博客--第六篇 note6
1、无论公司付给你多少,你的青春都是廉价的!!
2、通往财富自由之路 --得到APP
3、time
3.1 time.time()
t = time.time()
print(t) #--->1479138541.48461
3.2time.asctime()
f = time.asctime()
print(f) #---->Mon Nov 14 23:49:01 2016
3.3 time.localtime()
l = time.localtime()
print(l)
3.4时间的加减
l = time.localtime()
print(l)
g = time.localtime(time.time()-600)
print(g)
m = time.mktime(time.localtime())
print(m)
k = time.strptime('2014-11-02','%Y-%m-%d')
print(k)
p= time.strftime('%Y-%m-%d')
print(p) ##默认当前时间
print(time.strftime('%Y-%m-%d',time.localtime()))
lshutil.copy
copyfile
tar
import tarfile
des = tarfile.open(r'D:\BaiduYunDownload\1114.zip','w')
des.add(r'D:\BaiduYunDownload\day2_homework\day2_homework\three_menu',arcname='hbai')
des.close()
shelv
***
过滤文件:
shutil.copytree(r'D:\py15\py15_day1','test',ignore=shutil.ignore_patterns('*.md')) 这样可以过滤文件
XML
修改
删除:
config parser
纸打印自己和默认的
hashlib
import hashlib
m = hashlib.md5(b'alex123')
print(m.hexdigest())
m1=hashlib.md5()
m1.update(b'alex')
m1.update(b'123') ##追加
print(m1.hexdigest())
hmac
日志级别 不输出某些级别
log
Python 自带了re模块,它提供了对正则表达式的支持。主要用到的方法列
match 是从头开始匹配
\w 匹配字符
(25[0-5]|2[0-4]\d|[0-1]\d{2}|[1-9]?\d)\.(25[0-5]|2[0-4]\d|[0-1]\d{2}|[1-9]?\d)\.(25[0-5]|2[0-4]\d|[0-1]\d{2}|[1-9]?\d)\.(25[0-5]|2[0-4]\d|[0-1]\d{2}|[1-9]?\d)
4个斜杠 代表批评一个斜杠
匹配多行
1、匹配ip地址,\d{1,3}表示匹配一个数字,1到3位数字。 如果把192 改成1932,则匹配的位数必须含4,否则匹配不到,如果还是\d{1,3},则匹配不到1932;
下标点必须用转译符\
ip_addr = "inet 192.168.60.223 netmask 0xffffff00 broadcast 192.168.60.255"
m = re.search("\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}", ip_addr)
print(m.group())
2、匹配手机号
phone_str = "hey my name is alex, and my phone number is 13651054607, please call me if you are pretty!"
phone_str2 = "hey my name is alex, and my phone number is 18651054604, please call me if you are pretty!"
m = re.search("(1)([358]\d{9})",phone_str2)
if m:
print(m.group())
匹配方式是"(1)([358]\d{9})",以1开头,第二位是7358种的一个,后面匹配九个数字,共11位。
1[7358]\d{9} 这是单个号码的 1开头,第二位是7358中的一个,后面还有9个数字,共11位 。
/(1[7358]\d{9}\s*,\s*)*(1[7358]\d{9})/ 这是多个号码的 解释: 1[7358]\d{9} #匹配一个号码 \s*,\s* #匹配号码之间的逗号,号码和逗号之间允许有空格也就是\s* (1[7358]\d{9}\s*,\s*)* #整体合起来,*星号表示可以重复任意次,也就是可以有多个号码 (1[7358]\d{9}) #最后一个用来匹配没有逗号的号码
3、re.split # 将匹配到的格式当做分割点对字符串分割成列表
>>>m = re.split("[0-9]", "alex1rain2jack3helen rachel8")
>>>print(m)
***********************************
***********************************
有三层花括号,怎么用正则表达式匹配到最里层的花括话?
比如{1 2 3 { 4 5 { 6 7 } } }
要输出 6 7
>>> import re
>>> s="{1 2 3 { 4 5 { 6 7 } } } "
>>> reg=re.compile(r"\{[^{}]*\}")
>>> reg.search(s).group(0)
'{ 6 7 }'
>>>
**********************************
文本应该是这样的:
<SPAN><P>eng li aas<SS>ddde<AP>iiiiideeeeef<P>
怎样在Python中用正则表达式提取出尖括号之外的内容?
即最后的输出结果为eng li aas d dde iiiii deeeeef
最佳答案

**************************
rule1 = '\([^\(\)]+\)'
#取出内层括号内容 []内表示一对括号内 不包含 左括号或者右括号 ,整个表达式 是取在()内不包含左右括号的表达式。^ 表示非的意思,如果在[]外,表示以后边的字符开头,如果在[]内,表示非后边的字符。
rule2 = '[\-]{0,1}\d+[\.]{0,1}\d*[\*\/]{1}[\-]{0,1}\d+[\.]{0,1}\d*'#计算乘除 、
#rule2 = '[\-]{0,1}\d+[\.]{0,1}\d*[\*\/]{1}[\-]{0,1}\d+[\.]{0,1}\d*' 分三部分
#num1 = [\-]{0,1}\d+[\.]{0,1}\d* 比如 -1.2 或者2
#运算符号 = [\*\/]{1} 必须有一个乘号或除号
#num2 = [\-]{0,1}\d+[\.]{0,1}\d* 也是 -1.x或者
#是把乘号除号前后两个数字取出来,这两个数字可能为负的也可能带小数点
rule3 = '[\-]{0,1}\d+[\.]{0,1}\d*[\+\-]+\d+[\.]{0,1}\d*'#计算加减
***********正则表达式 参考链接
***********
http://www.jb51.net/article/65286.htm
import re
rule1 = 'abc'
pattern = re.compile(r'hello')
args = 'hello world! where is abcd'
res = re.search(pattern, args)
if res:
res = res.group()
print('result:%s'%res)#

浙公网安备 33010602011771号