python基础 (五)模块篇2
真心觉得python这门语言是业界良心:
- shelve模块
- xml处理
- configparser模块
- hashlib模块
- logging模块
一、shelve模块
shelve模块可以持久化任何pickle可支持的python数据格式,仅通过简单的key-value模式。
常规持久化:
#将创建好的类、列表、字典、时间戳等持久化
#运行后自动创建shelve_bak,shelve_dat,shelve_dir
import shelve
import datetime
d=shelve.open("shelve_test")
info={"age":21,"job":"it"}
age=[12,53,64]
#创建一个类
class staff(object):
def __init__(self,name):
self.name=name
#开始持久化
s1=staff("xzx")
s2=staff("mm")
d["info"]=info
d["age"]=age
d["s1"]=s1
d["s2"]=s2
d["date"]=datetime.datetime.now()
d.close()
常规读取持久化后的文件
#和pickle反序列化一样,对函数及类序列化,其定义和创建过程必须给出,因为当初创建函数和类后,内存中就抹去了,序列化文件中储存的只不过是一个内存地址,相当于空头支票
class staff(object):
def __init__(self,name):
self.name=name
#打开文件
d1=shelve.open("shelve_test")
#shelve的文件手柄d1具有的items,keys,values方法返回的都是迭代器
for i in d1.items():
print(i)
#读取s1.name
print(d1.get("s1").name)
输出结果:
#列出所有持续化数据
('name', {'alex', 'rain', 'test'})
('info', {'age': 21, 'job': 'it'})
('date', datetime.datetime(2017, 9, 11, 11, 55, 46, 795584))
('age', [12, 53, 64])
('s1', <__main__.staff object at 0x7f99f664ae10>)
('s2', <__main__.staff object at 0x7f99f6405ac8>)
#读取s1.name结果
xzx
二、 xml处理模块
xml是实现不同语言或程序之间进行数据交换的协议。现如今还剩了一些传统公司还在用xml,大家都转用json了。
下面是一个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协议在各个语言都是支持的,下面举例利用python中的xml模块来处理xml文件:
import xml.etree.ElementTree as ET
tree=ET.parse("location.xml")
root=tree.getroot()
#tag是节点的名称
print(root.tag)
#所有节点的name,attrib,text和tail,tail是结尾文本
for child in root:
for son in child:
print(son.tag,son.attrib,son.text,son.tail)
输出结果:
country {'name': 'Liechtenstein'}
rank {'updated': 'yes'} 2 dfgfdhgh
year {} 2008
gdppc {} 141100
country {'name': 'Singapore'}
rank {'updated': 'yes'} 5
year {} 2011
gdppc {} 59900
country {'name': 'Panama'}
rank {'updated': 'yes'} 69
year {} 2011
gdppc {} 13600
修改和删除xml文档内容:
import xml.etree.ElementTree as ET
tree=ET.parse("xmltest.xml")
root=tree.getroot()
#修改:
#所有的year.text加1
for node in root.iter("year"):
new_year=int(node.text)+1
node.text=str(new_year)
node.set("updated by","xmm")#给节点添加attrib属性
#将tree写到新文件中去,注意不能覆盖原文件,覆盖后原文件不能打开
tree.write("output.xml")
#删除:
for country in root.findall("country"):
rank=int(country.findtext("rank"))
if rank>50:
root.remove(country)
tree.write("newnew.xml")
自己创建xml文件,其实自己创建这个干嘛,了解下吧
import xml.etree.ElementTree as ET
new_xml=ET.Element("personinfolist")
personinfo=ET.SubElement(new_xml,"personinfo",attrib={"enrolled":"yes"})
age=ET.SubElement(personinfo,"age",attrib={"checked":"no"})
sex=ET.SubElement(personinfo,"sex")
sex.text="33"
personinfo2=ET.SubElement(new_xml,"personinfo",attrib={"enrolled":"no"})
name=ET.SubElement(personinfo2,"name")
name.text="xmm"
et=ET.ElementTree(new_xml)# 输入树的节点,攒成一棵树
et.write("test.xml",encoding="utf-8",xml_declaration=True)
ET.dump(new_xml)
输出结果:
<?xml version='1.0' encoding='utf-8'?>
<personinfolist>
<personinfo enrolled="yes">
<age checked="no" />
<sex>33</sex>
</personinfo>
<personinfo enrolled="no">
<name>xmm</name>
</personinfo>
</personinfolist>
三、configparser模块
用于生成和修改常见配置文档,下面为一个配置文档实例。
#配置文件:example.ini
[DEFAULT]
severaliveinterval = 45
compression = yes
compressionlevel = 9
forwardx11 = yes
[bitbucket.org]
user = hg
[tosecret.server.com]
host port = 50022
forwardx11 = no
这样一个文档可以由以下操作生成。
import configparser
config=configparser.ConfigParser()
config["DEFAULT"]={"SeverAliveInterval":"45","Compression":"yes","CompressionLevel":"9"}
config["bitbucket.org"]={}
config["bitbucket.org"]["User"]="hg"
config["tosecret.server.com"]={}
topsecret=config["tosecret.server.com"]
topsecret["Host Port"]="50022"
topsecret["forwardx11"]="no"
config["DEFAULT"]["Forwardx11"]="yes"
with open("example.ini","w") as configfile:
config.write(configfile)
读取配置文件操作:
import configparser
conf=configparser.ConfigParser()
print(conf.read("example.ini"))#判断能否读取.ini文件,若能读取返回该文件名
print(conf.sections())#返回小节名,不包括[DEFAULT]
print(conf.default_section)
print(conf.defaults())
print(conf.has_section("bitbucket.org"))
print(conf.options("bitbucket.org"))
print(conf.get("bitbucket.org","user"))
sec=conf.remove_section("bitbucket.org")#判断有无sections
print(sec)
with open("examle.cfg","w") as cfgfile:
conf.write(cfgfile)
四 、hashlib模块
用于加密相关的操作,主要提供 SHA1, SHA224, SHA256, SHA384, SHA512 ,MD5 算法
1、md5
import hashlib
m=hashlib.md5()
m.update(b"hello")
print(m.hexdigest())
m.update(b"easy go,if my body was on fire")
print(m.hexdigest())
m.update(b"you are a liar")
print(m.hexdigest())
#cefe459f41c025b72cfb1b9c65950a33
m2=hashlib.md5()
m2.update(b"helloeasy go,if my body was on fireyou are a liar")
print(m2.hexdigest())#加密是不断拼接的过程
#cefe459f41c025b72cfb1b9c65950a33
2、sha1
hash = hashlib.sha1()
hash.update('admin')
print(hash.hexdigest())
3、sha256
hash = hashlib.sha256()
hash.update('admin')
print(hash.hexdigest())
python中还有一个hmac模块,散列消息鉴别码,简称HMAC,是一种基于消息鉴别码MAC(Message Authentication Code)的鉴别机制。
五 、logging模块
python的logging模块提供了标准的日志接口,可以存储各种格式的日志,logging的日志可以分为debug(),info(),warning(),critical()5个级别。基本用法如下:
import logging
logging.warning("user [mm] attempted wrong more than 3 times")
logging.critical("server is down")
输出:
WARNING:root:user [mm] attempted wrong more than 3 times
CRITICAL:root:server is down
级别 | 用处 |
---|---|
debug | 打印全部的日志(notset等同于debug) |
info | 打印info,warning,error,critical级别的日志 |
warning | 打印warning,error,critical级别的日志 |
error | 打印error,critical级别的日志 |
critical | 打印critical级别 |
也可以把日志写到文件里:
其中下面这句中level=loggin.INFO意思,把日志记录级别设置为INFO,只有日志是INFO或INFO级别更高的日志
logging.basicConfig(filename='example.log',level=logging.INFO,format="%(asctime)s %(message)s",datefmt="%m%d%Y %I:%M:%S %p")
logging.debug('This message should go to the log file')
logging.info('so should this')
logging.warning('And this too')
日志格式:
format | interpretation | |
---|---|---|
%(name)s | Name of the logger (logging channel). | |
%(levelno)s | Numeric logging level for the message (DEBUG, INFO, WARNING, ERROR, CRITICAL). | |
%(levelname)s | Text logging level for the message (‘DEBUG’, ‘INFO’, ‘WARNING’, ‘ERROR’, ‘CRITICAL’). | |
%(pathname)s | Full pathname of the source file where the logging call was issued (if available). | |
%(filename)s | Filename portion of pathname. | |
%(module)s | Module (name portion of filename). | |
%(funcName)s | Name of function containing the logging call. | |
%(lineno)d | Source line number where the logging call was issued (if available). | |
%(created)f | Time when the LogRecord was created (as returned by time.time()). | |
%(relativeCreated)d | Time in milliseconds when the LogRecord was created, relative to the time the logging module was loaded. | |
%(asctime)s | Human-readable time when the LogRecord was created. By default this is of the form “2003-07-08 16:49:45,896” (the numbers after the comma are millisecond portion of the time). | |
%(msecs)d | Millisecond portion of the time when the LogRecord was created. | |
%(thread)d | Thread ID (if available). | |
%(threadName)s | Thread name (if available). | |
%(process)d | Process ID (if available). | |
%(message)s | The logged message, computed as msg % args. |