标签:
Python本身是没有正则表达式的,他是通过re模块来实现的。
Python的正则表达式(或 RE)是一种小型的、高度专业化的编程语言,它内嵌在Python中,并通过 re 模块实现。
>>> re.findall(‘hell..word‘,‘hello word‘) [‘hello word‘]
>>> re.findall(‘^hello‘,‘hello Wu,hello‘) [‘hello‘]
>>> re.findall(‘hello$‘,‘hello Wu,hello‘) [‘hello‘]
* ,重复出现0到多次,默认贪婪匹配,贪婪匹配就是从头匹配到结尾。
>>> re.findall(‘hello*‘,‘hellooooooooooasd‘) [‘helloooooooooo‘]
>>> re.findall(‘hello+‘,‘hellooo‘) [‘hellooo‘] >>> re.findall(‘hello+‘,‘hell‘) []
>>> re.findall(‘hello?‘,‘hell‘) [‘hell‘] >>> re.findall(‘hello?‘,‘hello‘) [‘hello‘] >>> re.findall(‘hello?‘,‘helloo‘) [‘hello‘]
>>> re.findall(‘hello{1,3}‘,‘helloo‘)#{}匹配1,3次 [‘helloo‘] >>> re.findall(‘hello{1,3}‘,‘helloooooo‘) [‘hellooo‘]
>>> re.findall(‘hel[lo]x‘,‘helox‘)#匹配hellx或者helox [‘helox‘]
re.findall(r"(ab)*","aba")或者re.search(r‘a(\d+)‘,‘a234234njka‘),可以再a(\d+?)
>>> re.findall("a|c","aba") [‘a‘, ‘a‘]
>>> re.findall(‘hello‘,‘asdkhfjlsadhhellosafsdf‘) [‘hello‘]
>>> re.search(‘hello‘,‘asdkhfjlsadhhellosafsdfhello‘).group() ‘hello‘
match(‘规则‘,‘字符串‘,‘工作方式‘) 如果有结果会返回,如果没有结果返回None,默认返回的是一个对象需要加.group()查看
>>> re.match(r‘blow‘, ‘blow123nlow‘) <_sre.SRE_Match object; span=(0, 4), match=‘blow‘> >>> re.match(r‘blow‘, ‘blow123nlow‘).group() ‘blow‘
>>> re.sub("g.t","have",‘I get A, I got B ,I gut C‘) ‘I have A, I have B ,I have C‘
>>> re.subn("g.t","have",‘I get A, I got B ,I gut C‘) (‘I have A, I have B ,I have C‘, 3)
p = re.compile(r‘\d+‘) p.split(‘one1two2three3four4‘)
>>> re.split(‘\d+‘,‘one1two2three3four4‘) [‘one‘, ‘two‘, ‘three‘, ‘four‘, ‘‘]
#模块 print(‘hello word‘)
import hello#导入模块hello print(‘Wu‘) 执行结果: hello word Wu
import module from module.xx.xx import xx from module.xx.xx import xx as rename from module.xx.xx import *
import sys print(sys.path) 执行结果: [‘F:\\oldboy-Python\\py_code\\test‘, ‘F:\\oldboy-Python‘, ‘C:\\Python35\\python35.zip‘, ‘C:\\Python35\\DLLs‘, ‘C:\\Python35\\lib‘, ‘C:\\Python35‘, ‘C:\\Users\\吴永奇\\AppData\\Roaming\\Python\\Python35\\site-packages‘, ‘C:\\Python35\\lib\\site-packages‘]
但是结果中没有我们要导入的模块的路径怎么办?这时候后我么可以使用sys.path.append(‘路径‘)来向寻找模块的路径中添加一个路径。
import sys sys.path.append(‘F:\\oldboy-Python\\py_code‘) print(sys.path) 执行结果: [‘F:\\oldboy-Python\\py_code\\test‘, ‘F:\\oldboy-Python‘, ‘C:\\Python35\\python35.zip‘, ‘C:\\Python35\\DLLs‘, ‘C:\\Python35\\lib‘, ‘C:\\Python35‘, ‘C:\\Users\\吴永奇\\AppData\\Roaming\\Python\\Python35\\site-packages‘, ‘C:\\Python35\\lib\\site-packages‘, ‘F:\\oldboy-Python\\py_code‘]
yum pip apt-get ...
下载源码
解压源码
进入目录
编译源码 python setup.py build
安装源码 python setup.py install
yum install gcc python-devel -y
或者
apt-get python-dev -y
import sys print(sys.argv) 执行结果: F:\oldboy-Python\py_code\test>python noke.py hello [‘noke.py‘, ‘hello‘]
import sys sys.exit(‘Good bye!‘) 执行结果: F:\oldboy-Python\py_code\test>python noke.py hello Good bye!
import sys print(sys.version) 执行结果: 3.5.1rc1 (v3.5.1rc1:948ef16a6951, Nov 22 2015, 23:41:41) [MSC v.1900 64 bit (AMD64)]
#Python2.7 import sys print(sys.maxint) 执行结果: 2147483647 #Python3.5 import sys print(sys.maxsize) 执行结果: 9223372036854775807
import sys print(sys.path) 执行结果: [‘F:\\oldboy-Python\\py_code\\test‘, ‘F:\\oldboy-Python‘, ‘C:\\Python35\\python35.zip‘, ‘C:\\Python35\\DLLs‘, ‘C:\\Python35\\lib‘, ‘C:\\Python35‘, ‘C:\\Users\\吴永奇\\AppData\\Roaming\\Python\\Python35\\site-packages‘, ‘C:\\Python35\\lib\\site-packages‘]
sys.platfrom,返回操作系统平台的名称
import sys print(sys.platform) 执行结果: win32
sys.stdout.write(‘please:‘) 执行结果: please:
>>> time.time()
1463652160.4625852
>>> time.ctime() ‘Thu May 19 18:05:20 2016‘
>>> time.ctime(time.time()-86400) ‘Wed May 18 18:05:40 2016‘
>>> time.gmtime(time.time()-86400)
time.struct_time(tm_year=2016, tm_mon=5, tm_mday=18, tm_hour=10, tm_min=6, tm_sec=10, tm_wday=2, tm_yday=139, tm_isdst=0)
>>> time.localtime(time.time())
time.struct_time(tm_year=2016, tm_mon=5, tm_mday=19, tm_hour=18, tm_min=6, tm_sec=41, tm_wday=3, tm_yday=140, tm_isdst=0)
1 >>> time.mktime(time.localtime()) 2 1463652428.0
>>> time.strftime("%Y-%m-%d %H:%M:%S",time.gmtime()) ‘2016-05-19 10:07:36‘
>>> time.strptime("2016-01-28","%Y-%m-%d") time.struct_time(tm_year=2016, tm_mon=1, tm_mday=28, tm_hour=0, tm_min=0, tm_sec=0, tm_wday=3, tm_yday=28, tm_isdst=-1)
import datetime print(datetime.date.today()) 执行结果: 2016-05-19
import datetime,time print(datetime.date.fromtimestamp(time.time()-86400)) 执行结果: 2016-05-18
import datetime,time print(datetime.datetime.now()) 执行结果: 2016-05-19 18:19:37.266536
import datetime,time print(datetime.datetime.now().timetuple()) 执行结果: time.struct_time(tm_year=2016, tm_mon=5, tm_mday=19, tm_hour=18, tm_min=20, tm_sec=16, tm_wday=3, tm_yday=140, tm_isdst=-1)
import datetime,time print(datetime.datetime.now().replace(2014,9,12)) 执行结果: 2014-09-12 18:20:49.646533
import datetime,time print(datetime.datetime.strptime("21/11/06 16:30", "%d/%m/%y %H:%M")) 执行结果: 2006-11-21 16:30:00
import datetime print(datetime.datetime.now() + datetime.timedelta(days=10)) 执行结果: 2016-05-29 18:21:57.771363
import datetime,time print(datetime.datetime.now() - datetime.timedelta(days=10)) 执行结果: 2016-05-09 18:22:29.773631
import datetime,time print(datetime.datetime.now() + datetime.timedelta(hours=10)) 执行结果: 2016-05-20 04:23:20.390089
import datetime,time print(datetime.datetime.now() + datetime.timedelta(weeks=1)) 执行结果: 2016-05-26 18:23:35.893500
import datetime print(datetime.datetime.now() + datetime.timedelta(minutes=12)) 执行结果: 2016-05-19 18:36:38.501280
import datetime print(datetime.datetime.now() + datetime.timedelta(milliseconds=10)) 执行结果: 2016-05-19 18:25:02.996842
import datetime print(datetime.datetime.now() + datetime.timedelta(microseconds=14)) 执行结果: 2016-05-19 18:25:38.922324
import datetime print(datetime.datetime.now() + datetime.timedelta(seconds=5)) 执行结果: 2016-05-19 18:28:25.656614
import pickle a=[1,2,3,4,5,6] b=pickle.dumps(a)#将数据通过特殊的方法转换为只有Python认识的字符串 print(b)
import pickle a=[1,2,3,4,5,6] b=open(‘db‘,‘wb‘) pickle.dump(a,b)将数据通过特殊形式的转换,转换成只有Python认识的字符串。
import json a=[1,2,3,4,5,6] b=json.dumps(a)#通过特殊的处理将字符串转换为所有语言都认识的字符串 print(b)
import json a=[1,2,3,4,5,6] b=open(‘db‘,‘w‘) json.dump(a,b)#通过特殊的处理将字符串转换为所有语言都认识的字符串,并写入文件
import random print(random.random()) 执行结果: 0.8696109071933436
import random print(random.randint(1, 2)) 执行结果: 1
impoer random print(random.randrange(1, 10)) 执行结果: 6
import random#随机模块 a=""#定义一个空的字符串 for i in range(4): #循环4次 num = random.randrange(0,4) #生成0-4的随机数 if num == 3 : #如果随机数是3那么就在验证码中生成一个0-9的数字 red2 = random.randrange(0,10) #自行百度ascii码对照表 a = a + str(red2)#向a变量中添加当前数字对应的ascii码的字符 elif num == 2 or num == 1: #如果随机数等于2或者1的时候生成小写字母 red3=random.randrange(97,123)#自行百度ascii码对照表 a = a + chr(red3)#向a变量中添加当前数字对应的ascii码的字符 else: #在验证码中生成一个随机的大写字母 red1 = random.randrange(65,91)#自行百度ascii码对照表 a = a + chr(red1)#向a变量中添加当前数字对应的ascii码的字符 print(a)#输出a变量
os.getcwd() 获取当前工作目录,即当前python脚本工作的目录路径 os.chdir("dirname") 改变当前脚本工作目录;相当于shell下cd os.curdir 返回当前目录: (‘.‘) os.pardir 获取当前目录的父目录字符串名:(‘..‘) os.makedirs(‘dir1/dir2‘) 可生成多层递归目录 os.removedirs(‘dirname1‘) 若目录为空,则删除,并递归到上一级目录,如若也为空,则删除,依此类推 os.mkdir(‘dirname‘) 生成单级目录;相当于shell中mkdir dirname os.rmdir(‘dirname‘) 删除单级空目录,若目录不为空则无法删除,报错;相当于shell中rmdir dirname os.listdir(‘dirname‘) 列出指定目录下的所有文件和子目录,包括隐藏文件,并以列表方式打印 os.remove() 删除一个文件 os.rename("oldname","new") 重命名文件/目录 os.stat(‘path/filename‘) 获取文件/目录信息 os.sep 操作系统特定的路径分隔符,win下为"\\",Linux下为"/" os.linesep 当前平台使用的行终止符,win下为"\t\n",Linux下为"\n" os.pathsep 用于分割文件路径的字符串 os.name 字符串指示当前使用平台。win->‘nt‘; Linux->‘posix‘ os.system("bash command") 运行shell命令,直接显示 os.environ 获取系统环境变量 os.path.abspath(path) 返回path规范化的绝对路径 os.path.split(path) 将path分割成目录和文件名二元组返回 os.path.dirname(path) 返回path的目录。其实就是os.path.split(path)的第一个元素 os.path.basename(path) 返回path最后的文件名。如何path以/或\结尾,那么就会返回空值。即os.path.split(path)的第二个元素 os.path.exists(path) 如果path存在,返回True;如果path不存在,返回False os.path.isabs(path) 如果path是绝对路径,返回True os.path.isfile(path) 如果path是一个存在的文件,返回True。否则返回False os.path.isdir(path) 如果path是一个存在的目录,则返回True。否则返回False os.path.join(path1[, path2[, ...]]) 将多个路径组合后返回,第一个绝对路径之前的参数将被忽略 os.path.getatime(path) 返回path所指向的文件或者目录的最后存取时间 os.path.getmtime(path) 返回path所指向的文件或者目录的最后修改时间
标签:
原文地址:http://www.cnblogs.com/WuYongQi/p/5505962.html