Shelve
XML processing
Configparser
Hashlib
Logging
Shelve module
Shelve is a simple k,v module that persists memory data through a file and can persist any Python data format that pickle can support
import
shelve
sw
=
shelve.
open
(
‘shelve_test.pkl‘
)
# 创建shelve对象
name
=
[
‘13‘
,
‘14‘
,
‘145‘
,
6
]
# 创建一个列表
dist_test
=
{
"k1"
:
"v1"
,
"k2"
:
"v2"
}
sw[
‘name‘
]
=
name
# 将列表持久化保存
sw[
‘dist_test‘
]
=
dist_test
sw.close()
# 关闭文件,必须要有
sr
=
shelve.
open
(
‘shelve_test.pkl‘
)
print
(sr[
‘name‘
])
# 读出列表
print
(sr[
‘dist_test‘
])
# 读出字典
sr.close()
XML processing Module
XML is a protocol that implements data exchange between different languages or programs, much like JSON, but JSON is simpler to use, but in ancient times, in the Dark Ages when JSON was not yet born, you could only choose to use XML, and so far many traditional companies, such as the financial industry, are mostly XML-like interfaces.
The format of XML is as follows: by the <> node to distinguish the data structure.
<?
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 protocols are supported in each language, and in Python you can manipulate XML with the following modules
Import
xml.etree.ElementTree as ET
tree
=
et.parse (
' test.xml '
)
# reads the XML file and saves it as an element object
root
=
tree.getroot ()
# get root
for
children
in
root:
# traverse the child tags under root
print
(Child.tag, Child.attrib)
# Print signature and properties
for
i
in
Child:
print
(I.tag, I.text, I.attrib)
# Print Label name values and properties
Output Result:
Country {' name ': ' Liechtenstein '}
Rank 2 {' Updated ': ' Yes '}
Year 2008 {}
GDPPC 141100 {}
Neighbor None {' Direction ': ' E ', ' name ': ' Austria '}
Neighbor None {' Direction ': ' W ', ' name ': ' Switzerland '}
Country {' name ': ' Singapore '}
Rank 5 {' Updated ': ' Yes '}
Year 2011 {}
GDPPC 59900 {}
Neighbor None {' Direction ': ' N ', ' name ': ' Malaysia '}
Country {' name ': ' Panama '}
Rank "{' Updated ': ' Yes '}
Year 2011 {}
GDPPC 13600 {}
Neighbor None {' Direction ': ' W ', ' name ': ' Costa Rica '}
Neighbor None {' Direction ': ' E ', ' name ': ' Colombia '}
We can also use tag names to get the contents of a category of tags
for
node
in
root.
iter
(
‘year‘
):
# 仅遍历标签名为year的标签
print
(node.tag, node.text, node.attrib)
Output Result:
Year 2008 {}
Year 2011 {}
Year 2011 {}
Common operations FOR XML
Modify:
import
xml.etree.ElementTree as ET
tree
=
ET.parse(
‘test.xml‘
)
# 读取xml文件,并以Element对象的形式保存
root
=
tree.getroot()
# 获取根
for
node
in
root.
iter
(
‘year‘
):
# 遍历year标签
node.text
=
str
(
int
(node.text)
+
1
)
# 将year标签的值+1,注意,读出来的标签的值都是字符串形式,注意数据类型转换
node.
set
(
‘updated‘
,
‘yes‘
)
# 更新该标签
tree.write(
‘test_2.xml‘
)
# 将结果写到文件,可以写到源文件也可以写到新的文件中
Delete:
import
xml.etree.ElementTree as ET
tree
=
ET.parse(
‘test.xml‘
)
# 读取xml文件,并以Element对象的形式保存
root
=
tree.getroot()
# 获取根
for
country
in
root.findall(
‘country‘
):
# 遍历所有country标签
rank
=
int
(country.find(
‘rank‘
).text)
# 在country标签查找名为rank的纸标签
if
rank >
50
:
# 判断如果rank标签的值大于50
root.remove(country)
# 删除该标签
tree.write(
‘test_3.xml‘
)
Description
The ITER method is used to find the final label, which is the label without the sub-label below, to get his value and attributes
The FindAll method is used to find child tags that also have child tags, and then to get the child label of the found label with the Find method of the object returned with Fandall
To create your own XML document
import
xml.etree.ElementTree as ET
new_xml
=
ET.Element(
"namelist"
)
# 新建根节点,或者说xml对象
name
=
ET.SubElement(new_xml,
"name"
,attrib
=
{
"enrolled"
:
"yes"
})
# 给新xml对象创建子标签
age
=
ET.SubElement(name,
"age"
,attrib
=
{
"checked"
:
"no"
})
# name标签在创建子标签age,attrib变量为属性
sex
=
ET.SubElement(name,
"sex"
)
sex.text
=
‘33‘
# 给标签赋值
name2
=
ET.SubElement(new_xml,
"name"
,attrib
=
{
"enrolled"
:
"no"
})
age
=
ET.SubElement(name2,
"age"
)
age.text
=
‘19‘
et
=
ET.ElementTree(new_xml)
#生成文档对象
et.write(
"test.xml"
, encoding
=
"utf-8"
,xml_declaration
=
True
)
# 将xml对象保存到文件xml_declaration表示xml文档的声明
ET.dump(new_xml)
#打印生成的格式
Configparser Module
The Configparser module is the package used to process the configuration file, the format of which is as follows: the brackets "[]" are included in the section. section below is a configuration content similar to Key-value. Many of the services are common in this format, like MySQL
Suppose we have such a configuration file
[DEFAULT]
Name = www.qq.com
[DBS]
Username = root
Passord = 123.com
Host = 127.0.0.1
[Server]
Name = www.baidu.com
Port = 80
Reading configuration Files
import
configparser
config
=
configparser.ConfigParser()
# 创建configparser对象
config.read(
‘example.ini‘
)
# 读取配置件
print
(config.sections())
# 获取所有的session
Output results
[' DBS ', ' server ']
Attention:
You can see there is no output default here, because the default session in Python has a special purpose, which is equivalent to all session defaults, that is, when a key and value are defined in default, when this does not exist in the session , the value of the key is the default definition of value
For example
print
(config[
‘dbs‘
][
‘name‘
])
The result of the output is
Www.qq.com
Description
You can see that the returned object after reading the configuration file is a bit like a dictionary, you can remove the value one by one from the configuration file by key, and even use the IN keyword to determine if the key exists
print
(
‘server‘
in
config)
Output results
True
Other common operations
Read:
print
(config.options(
‘dbs‘
))
# 获取某个session下的所有option,也就是key
Output results
[' username ', ' passord ', ' host ', ' name ']
print
(config.items(
‘dbs‘
))
# 获取某个session的键值列表,类似字典的items方法
Output results
[(' Name ', ' www.qq.com '), (' username ', ' root '), (' Passord ', ' 123.com '), (' Host ', ' 127.0.0.1 ')]
print
(config.get(
‘dbs‘
,
‘host‘
))
# 获取某个session下的某个option的值
Output results
127.0.0.1
port
=
config.getint(
‘server‘
,
‘port‘
)
# 获取某个session下的某个option的值,并以int的方式返回
print
(port)
print
(
type
(port))
A similar approach also has the getfloat and Getboolean methods, assuming that the value in the configuration file is the corresponding type, otherwise it will be an error
Description
Yes, true, 1, true, and so on in the configuration file, that is, True,no, False, 0, false, and so on, which is returned by Getboolean false.
Delete:
config.remove_option(
‘dbs‘
,
‘host‘
)
# 删除option
config.remove_section(
‘server‘
)
# 删除session
Hashlib Module
For cryptographic related operations, the 3.x replaces the MD5 module and the SHA module, mainly providing SHA1, SHA224, SHA256, SHA384, SHA512, MD5 algorithm
import
hashlib
m
=
hashlib.md5()
m.update(b
"Hello"
)
m.update(b
"It‘s me"
)
print
(m.digest())
m.update(b
"It‘s been a long time since last time we ..."
)
print
(m.digest())
#2进制格式hash
print
(
len
(m.hexdigest()))
#16进制格式hash
‘‘‘
def digest(self, *args, **kwargs): # real signature unknown
""" Return the digest value as a string of binary data. """
pass
def hexdigest(self, *args, **kwargs): # real signature unknown
""" Return the digest value as a string of hexadecimal digits. """
pass
‘‘‘
import
hashlib
# ######## md5 ########
hash
=
hashlib.md5()
hash
.update(
‘admin‘
)
print
(
hash
.hexdigest())
# ######## sha1 ########
hash
=
hashlib.sha1()
hash
.update(
‘admin‘
)
print
(
hash
.hexdigest())
# ######## sha256 ########
hash
=
hashlib.sha256()
hash
.update(
‘admin‘
)
print
(
hash
.hexdigest())
# ######## sha384 ########
hash
=
hashlib.sha384()
hash
.update(
‘admin‘
)
print
(
hash
.hexdigest())
# ######## sha512 ########
hash
=
hashlib.sha512()
hash
.update(
‘admin‘
)
print
(
hash
.hexdigest())
Logging module
Many programs have logging requirements, and the information contained in the log has a normal program Access log, there may be error logs, warning message output, logging module provides a standard log interface
Log level:
critcal = 50
ERROR = 40
Waring = 30
INFO = 20
DEBUG = 10
Set the logging level, this level (value) below the log (less than this value) will not be recorded.
import
logging
logging.warning(
"user [alex] attempted wrong password more than 3 times"
)#日志等级及日志内容
logging.critical(
"server is down"
)
#输出
WARNING:root:user [alex] attempted wrong password more than
3
times
CRITICAL:root:server
is
down
If you want to write the log in a file, it's easy.
import
logging
logging.basicConfig(filename
=
‘example.log‘
,level
=
logging.INFO)
logging.debug(
‘This message should go to the log file‘
)
logging.info(
‘So should this‘
)
logging.warning(
‘And this, too‘
)
One of the level=loggin in the following sentence. Info means that the logging level is set to info, that is, only logs that are higher than the log is info or the info level will be recorded in the file, in this case, the first log is not recorded, if you want to record the debug log, Change the log level to debug on the line.
logging.basicConfig(filename
=
‘example.log‘
,level
=
logging.INFO)
Feel the above log format forgot to add time, log do not know how to line, the following to add!
import
logging
logging.basicConfig(
format
=
‘%(asctime)s %(message)s‘
, datefmt
=
‘%m/%d/%Y %I:%M:%S %p‘
)
logging.warning(
‘is when this event was logged.‘
)
#输出
12
/
12
/
2010
11
:
46
:
36
AM
is
when this event was logged.
If you want to print log in the screen and file log at the same time
import
logging
#create logger
logger
=
logging.getLogger(
‘TEST-LOG‘
)
logger.setLevel(logging.DEBUG)
# create console handler and set level to debug
ch
=
logging.StreamHandler()
ch.setLevel(logging.DEBUG)
# create file handler and set level to warning
fh
=
logging.FileHandler(
"access.log"
)
fh.setLevel(logging.WARNING)
# create formatter
formatter
=
logging.Formatter(
‘%(asctime)s - %(name)s - %(levelname)s - %(message)s‘
)
# add formatter to ch and fh
ch.setFormatter(formatter)
fh.setFormatter(formatter)
# add ch and fh to logger
logger.addHandler(ch)
logger.addHandler(fh)
# ‘application‘ code
logger.debug(
‘debug message‘
)
logger.info(
‘info message‘
)
logger.warn(
‘warn message‘
)
logger.error(
‘error message‘
)
logger.critical(
‘critical message‘
)
Python's Common module (ii)