python中常用的模块的总结教学内容Word下载.docx
《python中常用的模块的总结教学内容Word下载.docx》由会员分享,可在线阅读,更多相关《python中常用的模块的总结教学内容Word下载.docx(16页珍藏版)》请在冰点文库上搜索。
module_name.py的路径---->
sys.path
导入包的本质就是执行该包下面的__init__.py
关于导入包的一个代码例子:
新建一个package_test包,并在该包下面建立一个test1.py的python程序,在package包的同级目录建立一个p_test.py的程序
test1的代码如下:
1deftest():
2print("
intthetest1"
)
package_test包下的__init__.py的代码如下:
1#importtest1(理论上这样就可以但是在pycharm下测试必须用下面from.importtest1)
2from.importtest1
intheinit"
p_test的代码如下:
1importpackage_test#执行__init__.py
2package_test.test1.test()
这样运行p_test的结果:
/python培训/s14/day5/p_test.py
2intheinit
3intthetest1
从上述的例子中也可以看出:
导入包的时候其实是执行包下的__init__.py程序,所以如果想要调用包下面的python程序需要在包下的__init__.py导入包下面的程序
2、模块的分类
a.标准库
b.开源模块
c.自动以模块
3、时间模块
time与datetime
python中常见的时间表示方法:
a.时间戳
时间戳:
从1970年1月1日00:
00:
00到现在为止一共的时间数(单位为秒)
>
time.time()
1472016249.393169
b.格式化的时间字符串
c.struct_time(元组)
相互之间的转换关系如下:
1)time.localtime()将时间戳转换为当前时间的元组
time.localtime()
time.struct_time(tm_year=2016,tm_mon=8,tm_mday=24,tm_hour=13,tm_min=27,tm_sec=55,tm_wday=2,tm_yday=237,tm_isdst=0)
2)time.gmtime()将时间戳转换为当前时间utc时间的元组
time.gmtime()
time.struct_time(tm_year=2016,tm_mon=8,tm_mday=24,tm_hour=5,tm_min=35,tm_sec=43,tm_wday=2,tm_yday=237,tm_isdst=0)
3)time.mktime()可以将struct_time转换成时间戳
x=time.localtime()
x
time.struct_time(tm_year=2016,tm_mon=8,tm_mday=24,tm_hour=13,tm_min=39,tm_sec=42,tm_wday=2,tm_yday=237,tm_isdst=0)
time.mktime(x)
1472017182.0
4)将struct_time装换成格式化的时间字符串
time.strftime("
%Y-%m-%d%H:
%M:
%S"
x)
'
2016-08-2413:
39:
42'
5)可以将格式化的时间字符串转换为struct_time
time.strptime("
2016-08-2414:
05:
32"
"
time.struct_time(tm_year=2016,tm_mon=8,tm_mday=24,tm_hour=14,tm_min=5,tm_sec=32,tm_wday=2,tm_yday=237,tm_isdst=-1)
6)将struct_time转换成WedAug2414:
22:
472016这种格式
time.struct_time(tm_year=2016,tm_mon=8,tm_mday=24,tm_hour=14,tm_min=22,tm_sec=47,tm_wday=2,tm_yday=237,tm_isdst=0)
time.asctime(x)
WedAug2414:
472016'
7)将时间戳装换成WedAug2414:
472016格式
x=time.time()
1472019984.958831
time.ctime(x)
26:
242016'
1%a本地(locale)简化星期名称
2%A本地完整星期名称
3%b本地简化月份名称
4%B本地完整月份名称
5%c本地相应的日期和时间表示
6%d一个月中的第几天(01-31)
7%H一天中的第几个小时(24小时制,00-23)
8%I第几个小时(12小时制,01-12)
9%j一年中的第几天(001-366)
10%m月份(01-12)
11%M分钟数(00-59)
12%p本地am或者pm的相应符
13%S秒(01-61)
14%U一年中的星期数。
(00-53星期天是一个星期的开始。
)第一个星期天之前的所有天数都放在第0周。
15%w一个星期中的第几天(0-6,0是星期天)
16%W和%U基本相同,不同的是%W以星期一为一个星期的开始。
17%x本地相应日期
18%X本地相应时间
19%y去掉世纪的年份(00-99)
20%Y完整的年份
21%Z时区的名字(如果不存在为空字符)
22%%‘%’字符
datetime
当前时间:
datetime.datetime.now()
1、随机模块random
random.randint(1,3)则可以取出随机1-3
random.randrange(1,3)随机从范围内所及
random.choice()传递的参数是序列包括字符串列表等
random.choice("
hello"
l'
o'
e'
random.choice(["
我"
爱"
你"
])
我'
你'
爱'
random.sample()随机从前面的序列取出两位
random.sample("
2)
['
'
]
h'
random的洗牌功能:
a=[1,2,3,4,5,6,7,8,9]
random.shuffle(a)
a
[6,3,7,4,1,8,9,2,5]
生成随机验证码的例子:
1importstring
2importrandom
3a="
"
.join(random.sample(string.ascii_lowercase,4))
4print(a)
5b="
.join(random.sample(string.ascii_lowercase+string.digits,5))
6print(b)
7
8c="
.join(random.sample(string.ascii_uppercase+string.digits+string.ascii_lowercase,4))
9print(c)
10d="
.join(random.sample(string.ascii_letters+string.digits,4))
11print(d)
运行结果如下:
/python培训/s14/day5/验证码2.py
2tbdy
36te4b
4Z2UA
5v8He
6
7Processfinishedwithexitcode0
5、os模块
1os.getcwd()获取当前工作目录,即当前python脚本工作的目录路径
2os.chdir("
dirname"
)改变当前脚本工作目录;
相当于shell下cd
3os.curdir返回当前目录:
('
.'
4os.pardir获取当前目录的父目录字符串名:
('
..'
5os.makedirs('
dirname1/dirname2'
)可生成多层递归目录
6os.removedirs('
dirname1'
)若目录为空,则删除,并递归到上一级目录,如若也为空,则删除,依此类推
7os.mkdir('
dirname'
)生成单级目录;
相当于shell中mkdirdirname
8os.rmdir('
)删除单级空目录,若目录不为空则无法删除,报错;
相当于shell中rmdirdirname
9os.listdir('
)列出指定目录下的所有文件和子目录,包括隐藏文件,并以列表方式打印
10os.remove()删除一个文件
11os.rename("
oldname"
newname"
)重命名文件/目录
12os.stat('
path/filename'
)获取文件/目录信息
13os.sep输出操作系统特定的路径分隔符,win下为"
\\"
Linux下为"
/"
14os.linesep输出当前平台使用的行终止符,win下为"
\t\n"
\n"
15os.pathsep输出用于分割文件路径的字符串
16os.name输出字符串指示当前使用平台。
win->
nt'
;
Linux->
posix'
17os.system("
bashcommand"
)运行shell命令,直接显示
18os.environ获取系统环境变量
19os.path.abspath(path)返回path规范化的绝对路径
20os.path.split(path)将path分割成目录和文件名二元组返回
21os.path.dirname(path)返回path的目录。
其实就是os.path.split(path)的第一个元素
22os.path.basename(path)返回path最后的文件名。
如何path以/或\结尾,那么就会返回空值。
即os.path.split(path)的第二个元素
23os.path.exists(path)如果path存在,返回True;
如果path不存在,返回False
24os.path.isabs(path)如果path是绝对路径,返回True
25os.path.isfile(path)如果path是一个存在的文件,返回True。
否则返回False
26os.path.isdir(path)如果path是一个存在的目录,则返回True。
27os.path.join(path1[,path2[,...]])将多个路径组合后返回,第一个绝对路径之前的参数将被忽略
28os.path.getatime(path)返回path所指向的文件或者目录的最后存取时间
29os.path.getmtime(path)返回path所指向的文件或者目录的最后修改时间
6、sys模块
1sys.argv命令行参数List,第一个元素是程序本身路径
2sys.exit(n)退出程序,正常退出时exit(0)
3sys.version获取Python解释程序的版本信息
4sys.path返回模块的搜索路径,初始化时使用PYTHONPATH环境变量的值
5sys.platform返回操作系统平台名称
6sys.stdout.write('
please:
7、shutil模块
1importshutil
2
3a=open("
a.txt"
r"
encoding="
utf-8"
4b=open("
b.txt"
w"
5
6shutil.copyfileobj(a,b)
运行够会复制一个文件b,将a文件中的内容复制到b文件中
shutil.copyfile("
c.txt"
)直接复制b.txt到c.txt
shutil.copymode(src,dst)仅拷贝权限。
内容,组,用户均不变
shutil.copystat(src,dst)拷贝状态的信息
shutil.copytree(src,dst,symlinks=false,ignore=none)递归拷贝文件
shutil.rmtree(path[,ignore_errors[,onerror]])
shutil.move(sr,dst)
递归移动文件
8、用于序列化的两个模块json&
pickle
json,用于字符串和python数据类型间进行转换
pickle,用于python特有的类型和python的数据类型间进行转换
Json模块提供了四个功能:
dumps、dump、loads、load
pickle模块提供了四个功能:
9、关于shelve模块
1#AUTHOR:
FAN
2importshelve
3importdatetime
4
5d=shelve.open("
shelve_test"
6
7info={"
name"
:
job"
it"
age"
23}
8
9d["
]=info["
10d["
11d["
date"
]=datetime.datetime.now()
12d.close()
运行结果,会生成如下三个文件
取出上述存的数据的代码如下:
1d=shelve.open("
2print(d.get("
))
3print(d.get("
4print(d.get("
/python培训/s14/day5/shelve模块/shelve_test.py
3it
42016-08-2416:
04:
13.325482
6Processfinishedwithexitcode0
10、正则re模块
1'
默认匹配除\n之外的任意一个字符,若指定flagDOTALL,则匹配任意字符,包括换行
2'
^'
匹配字符开头,若指定flagsMULTILINE,这种也可以匹配上(r"
^a"
\nabc\neee"
flags=re.MULTILINE)
3'
$'
匹配字符结尾,或e.search("
foo$"
bfoo\nsdfsf"
flags=re.MULTILINE).group()也可以
4'
*'
匹配*号前的字符0次或多次,re.findall("
ab*"
cabb3abcbbac"
)结果为['
abb'
ab'
a'
5'
+'
匹配前一个字符1次或多次,re.findall("
ab+"
ab+cd+abb+bba"
)结果['
6'
?
匹配前一个字符1次或0次
7'
{m}'
匹配前一个字符m次
8'
{n,m}'
匹配前一个字符n到m次,re.findall("
ab{1,3}"
abbabcabbcbbb"
)结果'
9'
|'
匹配|左或|右的字符,re.search("
abc|ABC"
ABCBabcCD"
).group()结果'
ABC'
10'
(...)'
分组匹配,re.search("
(abc){2}a(123|456)c"
"
abcabca456c"
).group()结果abcabca456c
11
12
13'
\A'
只从字符开头匹配,re.search("
\Aabc"
alexabc"
)是匹配不到的
14'
\Z'
匹配字符结尾,同$
15'
\d'
匹配数字0-9
16'
\D'
匹配非数字
17'
\w'
匹配[A-Za-z0-9]
18'
\W'
匹配非[A-Za-z0-9]
19'
\s'
匹配空白字符、\t、\n、\r,re.search("
\s+"
ab\tc1\n3"
).group()结果'
\t'
20'
\S'
匹配非空白字符
r代表取消引号里面特殊字符的意义
最常用的匹配语法:
re.match从头开始匹配
re.search匹配包含
re.findall把所有匹配到的字符放到以列表中的元素返回
re.splitall以匹配到的字符当做列表分隔符
re.sub匹配字符并替换
下面是关于正则的例子帮助理解:
1>
re.match("
^zhao"
zhaofan123"
2<
_sre.SRE_Matchobject;
span=(0,4),match='
zhao'
3>
^ww"
4>
5从这里也可以看出,如果有返回则表示匹配到了,否则则是没有匹配到
6>
res=re.match("
7>
res
8<
9>
res.group()#如果想要查看匹配的内容.group()
10'
11>
12匹配zhao后面以及数字
13>
^zhao\d"
zhao2323fan123"
14>
res.group()
15'
zhao2'
16匹配多个数字
17>
^zhao\d+"
18>
19'
zhao2323'
20>
21
22查找特定字符
23>
re.search("
f.+n"
24<
span=(8,11),match='
fan'
25>
26>
zhao2323fan123n"
27<
span=(8,15),match='
fan123n'
28>
29
30>
f[a-z]+n"
31<
32>
33$是匹配到字符串的最后