博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
python_day5
阅读量:35197 次
发布时间:2020-01-31

本文共 5389 字,大约阅读时间需要 17 分钟。

模块,用一砣代码实现了某个功能的代码集合。 

类似于函数式编程和面向过程编程,函数式编程则完成一个功能,其他代码用来调用即可,提供了代码的重用性和代码间的耦合。而对于一个复杂的功能来,可能需要多个函数才能完成(函数又可以在不同的.py文件中),n个 .py 文件组成的代码集合就称为模块。

常用的模块

1、time&datetime模块

    

1 #Author : Felix Li 2  3 import time 4  5 x=time.time()    # 时间戳  ( 从1970年到当前的秒数 unix 开始的时间) 6 print(x) 7  8 time.sleep(1) 9 y=time.clock()       #从进程开始,返回CPU的时间10 print(y)11 12 13 print(time.gmtime())  #时间戳转成struct_time ,结果为UTC时区14 z=time.localtime()    #时间戳转成struct_time ,结果为本地时区15 print(z)16 17 print(time.mktime(z))  #将struct_time转成时间戳18 19 c=time.strftime("%Y-%m-%d %H:%M:%S",z)   #将struct_time转成格式化的字符串形式20 print(c)21 22 print(time.strptime("2018/01/31 14:42:01","%Y/%m/%d %H:%M:%S"))  #将格式化的字符串转成struct_time
time

 

1 #Author : Felix Li 2  3 import datetime 4  5 print(datetime.datetime.now())      #当前时间 6  7 print(datetime.datetime.now() + datetime.timedelta(3))  #默认的是day 8  9 print(datetime.datetime.now()+datetime.timedelta(-15))     #当前时间的前15 day10 11 print(datetime.datetime.now()+datetime.timedelta(hours=3)) #当前时间+3 hours12 13 print(datetime.datetime.now()+datetime.timedelta(minutes=3))  #当前时间+3 分钟
datetime

2、random模块

 

1 #Author : Felix Li2 3 import random4 5 print(random.random())    #随机生成(0,1)之间的浮点数,顾头不顾尾!6 7 print(random.randint(0,2))  #随机生成整数0,1,28 9 print(random.randrange(0,2))  #随机生成0,1    和range 一样 顾头不顾尾!
random

 

1 #Author : Felix Li 2  3 import random 4  5 checkcode='' 6  7 for i in range(5): 8     current=random.randrange(0,5) 9     if current==i:10         number=chr(random.randint(97,122))11     elif current>i:12         number=chr(random.randint(65,90))13     else:14         number=random.randint(0,9)15     checkcode+=str(number)16 print(checkcode)
随机验证码

3、os模块

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

 

1 #Author : Felix Li 2 #  os模块  还得练习 3  4 import os 5  6 print(os.getcwd())  #获取当前工作目录,,即当前脚本工作目录 7  8 print(os.chdir(''))  #改变当前工作脚本 9 10 print(os.curdir)
os

4、shutil模块

    复制、压缩、解压文件 

 

1 #Author : Felix Li 2  3 import shutil  #复制的作用 4  5  6 f1=open('file_1',encoding="utf-8") 7 f2=open('file_2','w',encoding="utf-8") 8 shutil.copyfileobj(f1,f2) 9 10 11 shutil.copyfile("file_1","feil_3")12 13 14 shutil.rmtree("feil_3") #删除文件15 16 17 18 '''压缩文件 解压文件'''19 import zipfile20 z=zipfile.ZipFile('day5.zip','w')21 z.write("os_test")22 print('-------')23 z.write("shutil_test")24 z.close()
shutil

 

5、shelve模块

 

1 #Author : Felix Li 2 import shelve   #可以持久化任何pickle可支持的python数据格式 3 import datetime 4 d =shelve.open('shelve.test') 5  6 print(d.get('name')) 7 print(d.get('info')) 8 print(d.get('date')) 9 10 11 # info={'age':23,'job':'robots'}12 #13 # name=['felix','main','test']14 #15 # d['name']=name16 # d['info']=info17 # d['date']=datetime.datetime.now()
shelve

 

6、xml处理模块

    xml是实现不同语言或程序之间进行数据交换的协议,跟json差不多,但json使用起来更简单

 xml例子文件:

1  2     
3
2
4
2009
5
141100
6
7
8
9
10
5
11
2012
12
59900
13
14
15
16
69
17
2012
18
13600
19
20
21
22
xmltest
遍历xml:
1 #Author : Felix Li 2 import xml.etree.ElementTree as ET 3  4 tree=ET.parse('xml_text.xml') 5 root=tree.getroot()  #读取xml文件 6 print(root.tag) 7  8 for child in root:   #遍历xml文档 9     print(child.tag,child.attrib)10     for i in child:11         print(i.tag,i.text,i.attrib)12 13 14 for node in root.iter('gdppc'):  #只遍历gdppc15     print(node.tag,node.text)
xml_text

xml修改和删除:

1 #Author : Felix Li 2  3 import xml.etree.ElementTree as ET 4  5 tree = ET.parse("xml_text.xml") 6 root = tree.getroot() 7  8 # 修改 9 for node in root.iter('year'):10     new_year = int(node.text) + 111     node.text = str(new_year)12     node.set("updated", "yes")13 14 tree.write("xml_text.xml")15 16 # 删除node17 for country in root.findall('country'):18     rank = int(country.find('rank').text)19     if rank > 50:20         root.remove(country)21 22 tree.write('output.xml')
xml_修改和删除

 7、hashlib模块

    用于加密相关的操作,3.x里代替了md5模块和sha模块,主要提供 SHA1, SHA224, SHA256, SHA384, SHA512 ,MD5 算法。

例子中还包括hmac(内部对我们创建key和 '内容 '进行处理然后再加密):

1 #Author : Felix Li 2  3 import hashlib 4  5 m=hashlib.md5() 6 m.update('你是250'.encode(encoding="utf-8")) 7 print(m.digest()) 8 print(m.hexdigest()) 9 10 11 import hmac12 13 h=hmac.new('你是250'.encode(encoding="utf-8"))14 print(h.digest())  #加密成十进制15 print(h.hexdigest())#加密成十六进制
hashlisb

 

8、re模块(***)

 

#Author : Felix Liimport re # 匹配所有字符print(re.search(".+","we are family ! 123")) #匹配纯数字print(re.search("\d+","we are family ! 123")) #匹配前一个字符1次或者0次print(re.search("we are?","we are family ! 123")) #{m} 匹配前一个字符m次print(re.search("[0-9]{3}","we 4 are 5 family ! 123"))print(re.findall("[0-9]{1,3}","we 4 are 5 family ! 123")) #匹配左或者右的字符print(re.search("fa|we","we are family ! 123"))print(re.findall("we|fa|e","we are family ! 123")) #分组匹配print(re.search("(ly){2}(123|456)(\|\|){3}","we are familyly123|||||| ! 123")) #典型 匹配身份证号  (成了一个字典)print(re.search("(?P
[0-9]{2})(?P
[0-9]{2})(?P
[0-9]{2})(?P
[0-9]{8})","370123199406291018").groupdict()) #以匹配到的字符当作列表分隔符print(re.split("[0-9]","asd12fg44hh4rw33rr3")) #匹配字符并替换print(re.sub("e","E","we are family!!!"))
View Code

 

 

 

 

 

 

转载地址:http://hrtnmu.baihongyu.com/

你可能感兴趣的文章
【Leetcode刷题篇】leetcode703 数据流中的第k大元素
查看>>
【Leetcode刷题篇】leetcode378 有序矩阵中第K小的元素
查看>>
【Leetcode刷题篇】前K个高频元素
查看>>
【Leetcode刷题篇】leetcode373 查找和最小的K对数字
查看>>
【Leetcode刷题篇】leetcode367 有效的完全平方数
查看>>
【Leetcode刷题篇】剑指offer-数值的整数次方
查看>>
【Leetcode刷题篇】面试题01.06 字符串压缩
查看>>
【Leetcode刷题篇】leetcode443 压缩字符串
查看>>
【面试篇】数据结构-线性表
查看>>
【面试篇】数据结构-树形结构
查看>>
【面试篇】数据结构-哈希表
查看>>
【Leetcode刷题篇】leetcode88 合并两个有序数组
查看>>
【Leetcode刷题篇】剑指offer51 数组中的逆序对
查看>>
【Leetcode刷题篇】剑指offer55-平衡二叉树
查看>>
【Leetcode刷题篇】leetcode98 判断一棵树是否为二叉搜索树
查看>>
Java中arraylist和数组的相互转换
查看>>
【Leetcode刷题篇 】leetcode147 对链表进行插入排序
查看>>
【Leetcode刷题篇】leetcode148 排序链表
查看>>
【面试篇】Java中String、StringBuilder与StringBuffer的区别?
查看>>
【面试篇】Java对象的hashCode()相同,equals()一定为true吗?
查看>>