Python's Common module (ii)

Source: Internet
Author: User
Tags add time sha1

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

importshelve

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.

<?xmlversion="1.0"?>

<data>

<countryname="Liechtenstein">

<rankupdated="yes">2</rank>

<year>2008</year>

<gdppc>141100</gdppc>

<neighborname="Austria" direction="E"/>

<neighborname="Switzerland" direction="W"/>

</country>

<countryname="Singapore">

<rankupdated="yes">5</rank>

<year>2011</year>

<gdppc>59900</gdppc>

<neighborname="Malaysia" direction="N"/>

</country>

<countryname="Panama">

<rankupdated="yes">69</rank>

<year>2011</year>

<gdppc>13600</gdppc>

<neighborname="Costa Rica" direction="W"/>

<neighborname="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

fornode inroot.iter(‘year‘): # 仅遍历标签名为year的标签

print(node.tag, node.text, node.attrib)

Output Result:

Year 2008 {}

Year 2011 {}

Year 2011 {}

Common operations FOR XML

Modify:

importxml.etree.ElementTree as ET

tree =ET.parse(‘test.xml‘) # 读取xml文件,并以Element对象的形式保存

root =tree.getroot() # 获取根

fornode inroot.iter(‘year‘): # 遍历year标签

node.text =str(int(node.text) +1) # 将year标签的值+1,注意,读出来的标签的值都是字符串形式,注意数据类型转换

node.set(‘updated‘, ‘yes‘) # 更新该标签

tree.write(‘test_2.xml‘) # 将结果写到文件,可以写到源文件也可以写到新的文件中

Delete:

importxml.etree.ElementTree as ET

tree =ET.parse(‘test.xml‘) # 读取xml文件,并以Element对象的形式保存

root =tree.getroot() # 获取根

forcountry inroot.findall(‘country‘): # 遍历所有country标签

rank =int(country.find(‘rank‘).text) # 在country标签查找名为rank的纸标签

ifrank > 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

importxml.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

importconfigparser

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‘inconfig)

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

importhashlib

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

‘‘‘

importhashlib

# ######## 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.

importlogging

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 3times

CRITICAL:root:server isdown

If you want to write the log in a file, it's easy.

importlogging

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!

importlogging

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/201011:46:36 AM iswhen this event was logged.

If you want to print log in the screen and file log at the same time

importlogging

#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)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.