#什么是模块呢?就是用一大坨代码来完成一个功能的代码集合,是不是简单易懂
#类似于函数式编程和面向过程编程,函数式编程则完成一个功能,其他代码用来调用即可,提供了代码的重用性和代码间的耦合。而对于一个复杂的功能来,可能需要多个函数才能完成(函数又可以在不同的.py文件中),n个 .py 文件组成的代码集合就称为模块。
如:os 是系统相关的模块;file是文件操作相关的模块
模块分为三种:
- 自定义模块
- 内置标准模块(又称标准库)
- 开源模块
#既然别人可以写一个代码集合组成模块,我们自然也可以,这就是自定义模块
自定义模块 和开源模块的使用参考 http://www.cnblogs.com/wupeiqi/articles/4963027.html
1.json模块
用法:
import json
a = {
'':'a',
'':'b',
'':'c'
}
with open("1.txt",'r+') as f:
json.dump(a,f)
print(json.load(f))
>>>{'': 'a', '': 'b', '': 'c'}
f.write(json.dumps(a))
print(json.loads(f.readline()))
>>>{'': 'a', '': 'b', '': 'c'} #dump:将数据通过特殊的形式转换为所有程序语言都认识的字符串
,并放入文件,第一个参数是字典对象,第二个是文件对象
#dumps:将数据通过特殊的形式转换为所有程序语言都认识的字符串
#loads:将json编码的字符串再转换为python的数据结构
#load:从数据文件中读取数据,并将json编码的字符串转换为python的数据结构 #说明:
json编码支持的基本类型有:None, bool, int, float, string, list, tuple, dict. 对于字典,json会假设key是字符串(字典中的任何非字符串key都会在编码时转换为字符串),要符合JSON规范,应该只对python列表和字典进行编码。此外,在WEB应用中,把最顶层对象定义为字典是一种标准做法。 json编码的格式几乎和python语法一致,略有不同的是:True会被映射为true,False会被映射为false,None会被映射为null,元组()会被映射为列表[],因为其他语言没有元组的概念,只有数组,也就是列表。
a = {
'':True,
'':False,
'':None,
'':(1,2,3),
5:'qwe'
}
da = json.dumps(a)
print(da)
>>>{"": true, "": false, "": null, "": [1, 2, 3], "": "qwe"}
2. pickle模块
用法:
import pickle def hello():
for i in range(10):
print('Hello',i)
return hello
L = {
'a':1,
'b':2,
'c':'Daniel'
}
with open('pickle.txt','wb') as f:
f.write(pickle.dumps(hello))
with open('pickle.txt','wb') as f:
f.write(pickle.dumps(L)) #pickle可以序列化python所有的数据类型,但仅允许在python中使用(等于是python独有语法),与json不同,不管能否反序列化,都可以序列化
#如果你重新开个程序
import pickle
with open('pickle.txt','rb') as f:
a =pickle.loads(f.read())
print(a)
#则会报错'AttributeError: Can't get attribute 'hello' on <module '__main__'',因为你在前面那个程序定义了一个'hello',并且有它的内存地址,所以可以调用,但是到了这个新程序就没有了,要想调用,之你那个将''hello()''复制到新程序
#那如果调用‘L’则毫无问题,因为存的不是内存地址,而是数据
#所以说内存地址用完则释放,两个程序无法互相访问内存地址,数据可以直接反序列化 #当然pickle也有'load'、'dump'
L = {
'a':1,
'b':2,
'c':'Daniel'
}
with open('pickle.txt','wb') as f:
pickle.dump(L,f)
with open('pickle.txt','rb') as f:
a = pickle.load(f)
print(a)
>>>{'a': 1, 'b': 2, 'c': 'Daniel'}
3.time & datetime模块
用法:
import time print(time.clock()) #返回处理器时间,3.3开始已废弃 , 改成了time.process_time()测量处理器运算时间,不包括sleep时间,不稳定,mac上测不出来
print(time.altzone) #返回与utc时间的时间差,以秒计算\
print(time.asctime()) #返回时间格式"Mon Nov 6 17:21:39 2017"
print(time.localtime()) #返回本地时间 的struct time对象格式
print(time.gmtime(time.time()-800000)) #返回utc时间的struc时间对象格式
print(time.asctime(time.localtime())) #返回时间格式"Mon Nov 6 17:31:19 2017"
print(time.ctime()) 同上 # 日期字符串 转成 时间戳 string_struct = time.strptime('2017/11/13',"%Y/%m/%d") #将 日期字符串 转成 struct时间对象格式
print(string_struct)
str_stamp = time.mktime(string_struct) #将struct时间对象转成时间戳
print(str_stamp) #时间戳转成日期字符串 print(time.gmtime(time.time()-86640))
print(time.strftime("%Y-%m-%d %H:%M:%S",time.gmtime())) #时间加减 print(datetime.datetime.now())
>>>2017-11-13 17:33:00.117320
print(datetime.date.fromtimestamp(time.time()))
>>>2017-11-13 #时间戳直接转成日期字符串
print(datetime.datetime.now() + datetime.timedelta(3)) #加三天
>>>2017-11-16 17:33:00.117320
print(datetime.datetime.now() + datetime.timedelta(-3)) #减三天
>>>2017-11-10 17:33:00.117320
print(datetime.datetime.now() + datetime.timedelta(hours=1))
>>>2017-11-13 18:33:00.117320 #加一小时
print(datetime.datetime.now() + datetime.timedelta(hours=-1))
>>>2017-11-13 16:33:00.117320 #减一小时
print(datetime.datetime.now()+datetime.timedelta(minutes=10)) #加十分钟
>>>2017-11-13 17:43:00.117320
#so 那么就有秒----"second"不举例子了。=
print(now_time.replace(minute=0,hour=18,second=0,microsecond=0))
>>>2017-11-13 18:00:00 #时间替换
4.random模块
用法:
import random #生成随机数
print(random.random())
print(random.randint(1,10000)) #给随机数指定范围,最高为10000
print(random.randrange(1,10000000000)) #给随机数指定范围,最高9999999999 #生成随机验证码 import random
ver_code = ''
for i in range(5):
number = random.randrange(0,5)
if number != i:
letter = chr(random.randrange(65,99))
else:
letter = random.randint(0,9)
ver_code += str(letter)
print(ver_code)
5.os模块
提供对操作系统进行调用的接口
用法:
import os print(os.getcwd())
>>>c:\test.py #查看当前路径(类似shell的pwd)
os.chdir('test')
print(os.getcwd())
>>>c:\test\test2 #改变当前路径(类似shell的cd)
os.curdir #返回当前目录 (类似shell的cd .)
os.pardir #返回父级目录(类似shell的 cd ..)
os.makedirs('P1/P2/P3') #创建多级目录
os.removedirs('A/B/C') #删除多级目录,比如(A/B/C) 如果C是空的则删除,不是则不删除,并返回错误,依次往上一级推
os.mkdir('test') #创建目录('mkdir')
os.rmdir('test') #删除目录,不为空则删除失败
os.listdir('test') #列出目录下的所有文件和所有目录
os.remove('test') #删除一个文件
os.rename('oldtest','newtest') #修改文件或目录名字
print(os.stat('oldtest') #获取文件或目录信息
print(os.sep ) #返回系统路径分隔符
print(os.linesep) #返回行的终止符
print(os.pathsep ) #返回分割文件路径的字符串
os.environ #获取系统环境变量
os.system("ping 1.1.1.1") #直接执行shell的命令
print(os.path.abspath('C:')) #返回规范化的绝对路径
print(os.path.split('c:/test'))
>>>('c:', 'test') #将目录和文件以二元组的方式返回
print(os.path.dirname(path)) #返回父级目录,即'os.path.split'的第一个元素
print(os.path.basename('path')) #返回结束的文件名,即'os.path.split'的第二个元素
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所指向的文件或者目录的最后修改时间
6.sys模块
用法:
sys.argv #配合脚本使用可以读取从外面传进来的参数
print(sys.version) #查看python版本
sys.exit() #退出程序
sys.path #返回模块搜索路径,还有python环境变量
#还有modules,stdin,stdout,大家自己去查吧
7.shutil模块
用法:
import shutil f1 = open('1.txt')
f2 = open('2.txt','w',encoding='utf-8')
shutil.copyfileobj(f1,f2) #复制文件
shutil.copyfile('1.txt','2.txt') #可直接复制
shutil.copytree('','') #复制目录,整个目录
shutil.rmtree('') #删除目录
shutil.move(src,stc) #移动
shutil.make_archive('C:/1','zip','D:/test') #压缩,打包 #还有两种压缩方式:'Zipfile','Tarfile' Zipfile:
压缩:
z = zipfile.ZipFile('test.zip','w') #一个一个选择压缩
z.write('test1.py')
z.write('test2.py')
z.close()
解压:
z = zipfile.ZipFile('test.zip','r')
z.extractall()
z.close() Tarfile:
#跟上面差不多,举个例子
tar = tarfile.open('1.tar','w)
tar.add('test1.py')
tar.close()
#解压跟上面完全一样
tar.extractall()
tar.close()
8.shelve模块
shelve是一个简单的的key,vlaue将内存数据通过文件持久化的模块,可以持久化任何pickle可支持的python数据格式
用法:
import shelve d = shelve.open('shelve_test') # 打开一个文件 class Test(object):
def __init__(self, n):
self.n = n t = Test(123)
t2 = Test(123334) name = ["alex", "rain", "test"]
d["test"] = name # 持久化列表
d["t1"] = t # 持久化类
d["t2"] = t2 d.close()
d = shelve.open('shelve_test') #读取
print(d.get('test'))
>>>['alex', 'rain', 'test']
9.xml模块
xml是实现不同语言或程序之间进行数据交换的协议,跟json差不多,但json使用起来更简单,不过,古时候,在json还没诞生的黑暗年代,大家只能选择用xml呀,至今很多传统公司如金融行业的很多系统的接口还主要是xml。
xml的格式如下,就是通过<>节点来区别数据结构的:
<?xml version="1.0"?>
<data>
<country name="Liechtenstein">
<rank updated="yes">2</rank>
<year>2008</year>
<gdppc>141100</gdppc>
<neighbor name="Austria" direction="E"/>
<neighbor name="Switzerland" direction="W"/>
</country>
<country name="Singapore">
<rank updated="yes">5</rank>
<year>2011</year>
<gdppc>59900</gdppc>
<neighbor name="Malaysia" direction="N"/>
</country>
<country name="Panama">
<rank updated="yes">69</rank>
<year>2011</year>
<gdppc>13600</gdppc>
<neighbor name="Costa Rica" direction="W"/>
<neighbor name="Colombia" direction="E"/>
</country>
</data>
XML文件
import xml.etree.ElementTree as ET
tree = ET.parse("XML")
root = tree.getroot() #读取全部
print(root.tag)
for child in root:
print(child.tag, child.attrib)
for i in child:
print(i.tag,i.text) #只读取其中一项比如(year)
for node in root.iter('year'):
print(node.tag,node.text) #修改xml文件的内容
for node in root.iter('year'):
new_year = int(node.text) + 1
node.text = str(new_year)
node.set("updated", "yes") tree.write("XML") #将year+1 #删除xml文件的内容
for country in root.findall('country'):
rank = int(country.find('rank').text)
if rank > 50:
root.remove(country) tree.write('output.xml') #如果country下的rank大于50就删除country #有删有改有查自然也有增
new_xml = ET.Element('company')
post = ET.SubElement(new_xml,'post',attrib={"enrolled":"yes"})
name = ET.SubElement(post,'name',attrib={"ehecked":"no"})
age = ET.SubElement(name,'age')
post.text = 'General manager'
name.text = 'Daniel'
age.text = ''
et = ET.ElementTree(new_xml)
et.write('XML',encoding='utf-8',xml_declaration=True)
ET.dump(new_xml)
处理XML文件
10.PyYaml
Python也可以很容易的处理ymal文档格式,只不过需要安装一个模块,参考文档:http://pyyaml.org/wiki/PyYAMLDocumentation