#----正则表达式
import re elink = ‘<a href="(.*)">(.*)</a>‘ info = ‘<a href="http://www.baidu.com">baidu</a>‘ cinfo = re.findall(elink,info) print (cinfo)
import re print(re.search (r‘^a‘,‘abc\neee‘)) #预期结果 ^匹配字符开头 # <_sre.SRE_Match object; span=(0, 1), match=‘a‘> data=(re.match(‘^zhang‘,‘zhangyazhang‘)) print(data) #<_sre.SRE_Match object; span=(0, 5), match=‘zhang‘> data.group() #获取到zhang这个值。
res=(re.match(‘^zhang\d‘,‘zhang123yazhang‘)) print(res) #<_sre.SRE_Match object; span=(0, 6), match=‘zhang1‘> res=(re.match(‘^zhang\d+‘,‘zhang123yazhang‘)) print(res) #<_sre.SRE_Match object; span=(0, 8), match=‘zhang123‘> res=(re.match(‘.+‘,‘zhang123yazhang‘)) print(res) #匹配所有字符 #<_sre.SRE_Match object; span=(0, 15), match=‘zhang123yazhang‘> data=re.search(‘a.+d$‘,‘zhangabcd‘) print(data) #$匹配最后一个字符 #<_sre.SRE_Match object; span=(2, 9), match=‘angabcd‘> print(re.findall("ab+","ab+cd+abb+bba")) #[‘ab‘, ‘abb‘] print(re.findall("ab*","cabcabb3bbac") ) #--匹配*号前的字符0次或多次 (解释*号前是b,匹配b 0次或多次,a是前面必须有的,匹配b零次) #[‘ab‘, ‘abb‘, ‘a‘] print(re.findall(r‘\d+‘,‘a512b6‘)) #[‘512‘, ‘6‘] print(re.findall(r‘\d+‘,‘one1two2three3four4‘)) #[‘1‘, ‘2‘, ‘3‘, ‘4‘]
#-----sub的用法 --把content中的内容按link的模式替换成www.cnpythoner.com import re link = ("\d+") content = "laowang-222haha" info = re.sub(link,‘www.cnpythoner.com‘,content) print (info) #laowang-www.cnpythoner.comhaha 等价于 print(re.sub("\d+",‘www.cnpythoner.com‘,"laowang-222haha"))
导入包的质是执行包下的_init_.py文件 #bao\_init.py def day_test(): print("in the day_test") day_test() #bao_test.py
import bao
import导入模块 #--------main.py def test(): print("in the test ") test() #--------module1.py name = ‘alex‘ def test2(): print("in the test2") test2() #---module.py执行 #import main,module1 # module1.test2() # print(module1.name) #---------另一种方式module.py执行 # from main import test # from module1 import test2 # test() # test2()
import re,time print(re.search("abc|ABC","ab1cABCBCD").group()) #ABC print(re.search("[A-Za-z0-9]a","0aAB").group())#0aAB print(re.findall("[0-9]{1,3}","aa1x2a34567")) #[‘1‘, ‘2‘, ‘345‘, ‘67‘] print(re.search("(?P<province>[0-9]{4})(?P<city>[0-9]{2})(?P<birthday>[0-9]{4})","371419938106143242").groupdict("birthday")) {‘province‘: ‘3714‘, ‘birthday‘: ‘9381‘, ‘city‘: ‘19‘} #‘\n‘是换行,‘\t‘是tab,‘\\‘是\ print(re.search("(?P<id>[0-9]+)","[email protected]").group()) #1234 print(re.search("(?P<id>[0-9]+)","[email protected]").groupdict(id)) #{‘id‘: ‘1234‘} print(re.split("[0-9]+","avb12fsd2ff3tt5D"))#[‘avb‘, ‘fsd‘, ‘ff‘, ‘tt‘, ‘D‘] print(re.sub("[0-9]+","|","ab1ee888iii0BBVV9$%"))#ab|ee|iii|BBVV|$% print(re.sub("[0-9]+","|","ab1ee888iii0BBVV9$%",count=2))#ab|ee|iii0BBVV9$% import re print (re.search("\s+", "ab- \r\n ") ) #<_sre.SRE_Match object; span=(3, 8), match=‘ \r\n ‘>
表示时间:1)时间戳 2)格式化的时间字符串 3)元组(struct_time) import time res=time.time() print(res)#1472142901.0856912 x=res/3600/24/365 #46.681345163802995 print(x) #1970+46=2016 print(time.localtime(323233211)) x=323233211/3600/24/365 print(x)#1970+10=1980 时间戳表示的是从1970年1月1日00:00:00开始按秒计算的偏移量 y=time.localtime(323233211) print(y) print(y.tm_year) #y=1980 y=time.localtime(323233211) print(y)#struct print(time.mktime(y))#元组转换成时间戳 323233211.0 #print(time.asctime()) #等于print(time.asctime(time.localtime())) #Thu Aug 25 21:55:06 2016 print(time.asctime()) #等于print(time.asctime(time.localtime())) #Thu Aug 25 21:55:06 2016 data=time.strptime("2016/08/25","%Y/%m/%d") #将日期字符串 转成 struct时间对象格式 print(data) res=time.mktime(data) #将struct时间对象转成时间戳 print(res)
import datetime,time print(datetime.datetime.now()) #2016-08-25 23:42:44.481994 print(datetime.date.fromtimestamp(time.time()) ) #2016-08-25 print(datetime.datetime.now()+datetime.timedelta(hours=3)) #当前时间+3小时 print(datetime.datetime.now()+datetime.timedelta(3)) #当前时间+3天 print(datetime.datetime.now()+datetime.timedelta(minutes=3))#当前时间+3分钟 c_time = datetime.datetime.now() print(c_time) #打印当前时间 print(c_time.replace(minute=3,hour=2)) #时间替换
时间: 2024-10-07 13:29:37