首页 > 技术文章 > PYTHON之路(五)

joey251744647 2016-02-27 16:00 原文


正则表达式

用compile方法先生成要匹配的正则对象

 

相当于m=re.match("^[0-9]","18ruue");print m.group()


re.match
re.search
re.sub
re.findall
re.split

re.search ---- > group

 



re.split


re.findall


re.sub




冒泡排序




模块
模块中供别人调用的方法所在的文件最好不要执行初始化对象,比如下面例子中的tv()
index.py 执行会报错,说没有那个模块





这个文件会被main.py所调用,对于main.py来说,它能识别backend.db.sqlApi.select, 因为它的当前路径sys.path下一级即是backend.所以它调用index.py不出错,还把index.py里的语句tv()也执行了。


正确写法是去掉tv()在index.py



从自己往上级或者上上级调用该怎么办?
goUpper.py uses from conf import settings , 可以看到conf目录是goUpper.py的上上级了,而conf在selfDefineModule目录之下,因此需要把selfDefineModule目录加入到sys.path.
__file__是打印相对路径,os.path.abspath()打印绝对路径, os.path.dirname()是打印前一层目录

os.path.dirname()
os.path.abspath(__file__)



time and datetime module

import time
import datetime

print(time.clock()) #返回处理器时间,3.3开始已废弃
print(time.process_time()) #返回处理器时间,3.3开始已废弃
print(time.time()) #返回当前系统时间戳
print(time.ctime()) #输出Tue Jan 26 18:23:48 2016 ,当前系统时间
print(time.ctime(time.time()-86640)) #将时间戳转为字符串格式
print(time.gmtime(time.time()-86640)) #将时间戳转换成struct_time格式
print(time.localtime(time.time()-86640)) #将时间戳转换成struct_time格式,但返回 的本地时间
print(time.mktime(time.localtime())) #与time.localtime()功能相反,将struct_time格式转回成时间戳格式
#time.sleep(4) #sleep
print(time.strftime("%Y-%m-%d %H:%M:%S",time.gmtime()) ) #将struct_time格式转成指定的字符串格式
print(time.strptime("2016-01-28","%Y-%m-%d") ) #将字符串格式转换成struct_time格式

#datetime module

print(datetime.date.today()) #输出格式 2016-01-26
print(datetime.date.fromtimestamp(time.time()-864400) ) #2016-01-16 将时间戳转成日期格式
current_time = datetime.datetime.now() #
print(current_time) #输出2016-01-26 19:04:30.335935
print(current_time.timetuple()) #返回struct_time格式

#datetime.replace([year[, month[, day[, hour[, minute[, second[, microsecond[, tzinfo]]]]]]]])
print(current_time.replace(2014,9,12)) #输出2014-09-12 19:06:24.074900,返回当前时间,但指定的值将被替换

str_to_date = datetime.datetime.strptime("21/11/06 16:30", "%d/%m/%y %H:%M") #将字符串转换成日期格式
new_date = datetime.datetime.now() + datetime.timedelta(days=10) #比现在加10天
new_date = datetime.datetime.now() + datetime.timedelta(days=-10) #比现在减10天
new_date = datetime.datetime.now() + datetime.timedelta(hours=-10) #比现在减10小时
new_date = datetime.datetime.now() + datetime.timedelta(seconds=120) #比现在+120s
print(new_date)







random module

random.random()
random.randint(1,5)
randon.randrange(1,5)




import random
import re
bb=''

for i in range(4):
aa = random.randint(0,4)
if aa != i:
tmp = chr(random.randrange(65,90))
else:
tmp = random.randint(0,9)
bb+=str(tmp)
print(bb)
a = re.findall("[0-9]",bb)
b = re.findall("[A-Z]",bb)
if a:
if len(a) > 2:
bb=re.sub("[0-9]",str(chr(random.randrange(65,90))),bb,count=1)
if b:
if len(b) > 2:
bb=re.sub("[A-Z]",str(random.randrange(0,9)),bb,count=1)


print(bb)






OS模块  
提供对操作系统进行调用的接口


os.getcwd() 获取当前工作目录,即当前python脚本工作的目录路径
os.chdir("dirname") 改变当前脚本工作目录;相当于shell下cd
os.curdir 返回当前目录: ('.')
os.pardir 获取当前目录的父目录字符串名:('..')
os.makedirs('dirname1/dirname2') 可生成多层递归目录
os.removedirs('dirname1') 若目录为空,则删除,并递归到上一级目录,如若也为空,则删除,依此类推
os.mkdir('dirname') 生成单级目录;相当于shell中mkdir dirname
os.rmdir('dirname') 删除单级空目录,若目录不为空则无法删除,报错;相当于shell中rmdir dirname
os.listdir('dirname') 列出指定目录下的所有文件和子目录,包括隐藏文件,并以列表方式打印
os.remove() 删除一个文件
os.rename("oldname","newname") 重命名文件/目录
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所指向的文件或者目录的最后修改时间





sys模块

sys.argv 命令行参数List,第一个元素是程序本身路径
sys.exit(n) 退出程序,正常退出时exit(0)
sys.version 获取Python解释程序的版本信息
sys.maxint 最大的Int值
sys.path 返回模块的搜索路径,初始化时使用PYTHONPATH环境变量的值
sys.platform 返回操作系统平台名称
sys.stdout.write('please:')
val = sys.stdin.readline()[:-1]











sys.stdout.flush()是让#逐个打印,而不是全部输出到缓冲区后最后一起打印




pickle and json
pickle only exists in python; pickle supports many data types, such as dict, list, function, module
json supports all language; json supports basic data type





http://www.cnblogs.com/alex3714/articles/5161349.html


推荐阅读