Python learning road _day_19 (commonly used module 2)

Common modules

1、XML

2、shelve

3、configparser

 

1. What is the shelve module:

Instructions:

1、open

2. Read and write

3、close

Features: The use method is relatively simple, providing a text file name to start reading and writing

      The method of reading and writing is consistent with the dictionary

      Think of it as a dictionary with automatic serialization

Principle: Pickle is used internally, so there is also poor cross-platform compatibility

      Only you know how to get it

When to use: Consider writing a stand-alone program

 

 The shelve module is simpler than the pickle module, with only one open function, which returns a dictionary-like object, readable and writable; the key must be a string, and the value can be a data type supported by python

 

import shelve

 

f=shelve.open(r'sheve.txt')

# f['stu1_info']={'name':'egon','age':18,'hobby':['piao','smoking','drinking']}

# f['stu2_info']={'name':'gangdan','age':53}

# f['school_info']={'website':'http://www.pypy.org','city':'beijing'}

 

print(f['stu1_info']['hobby'])

f.close()

 

2. XML module

XML is a protocol for realizing data exchange between different languages ​​or programs. It is similar to JSON, but JSON is easier to use. However, in ancient times, in the dark age when JSON was not born, everyone could only choose to use XML. The interface of many systems in traditional companies such as the financial industry is still mainly 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协议在各个语言里的都 是支持的,在python中可以用以下模块操作xml:

# print(root.iter('year')) #全文搜索

# print(root.find('country')) #在root的子节点找,只找一个

# print(root.findall('country')) #在root的子节点找,找所有

 

3. configparser模块

配置文件如下:

 

# 注释1; 注释2

 

[section1]

k1 = v1

k2:v2

user=egon

age=18

is_admin=true

salary=31
[section2]

k1 = v1

 

读取

 

import configparser

 

config=configparser.ConfigParser()

config.read('a.cfg')

#查看所有的标题

res=config.sections() #['section1', 'section2']print(res)

#查看标题section1下所有key=value的key

options=config.options('section1')print(options) #['k1', 'k2', 'user', 'age', 'is_admin', 'salary']

#查看标题section1下所有key=value的(key,value)格式

item_list=config.items('section1')print(item_list) #[('k1', 'v1'), ('k2', 'v2'), ('user', 'egon'), ('age', '18'), ('is_admin', 'true'), ('salary', '31')]

#查看标题section1下user的值=>字符串格式

val=config.get('section1','user')print(val) #egon

#查看标题section1下age的值=>整数格式

val1=config.getint('section1','age')print(val1) #18

#查看标题section1下is_admin的值=>布尔值格式

val2=config.getboolean('section1','is_admin')print(val2) #True

#查看标题section1下salary的值=>浮点型格式

val3=config.getfloat('section1','salary')print(val3) #31.0

 

4. hashlib模块

# 1、什么叫hash:hash是一种算法(3.x里代替了md5模块和sha模块,主要提供 SHA1, SHA224, SHA256, SHA384, SHA512 ,MD5 算法),该算法接受传入的内容,经过运算得到一串hash值

# 2、hash值的特点是:

#2.1 只要传入的内容一样,得到的hash值必然一样=====>要用明文传输密码文件完整性校验

#2.2 不能由hash值返解成内容=======》把密码做成hash值,不应该在网络传输明文密码

#2.3 只要使用的hash算法不变,无论校验的内容有多大,得到的hash值长度是固定的

 hash算法就像一座工厂,工厂接收你送来的原材料(可以用m.update()为工厂运送原材料),经过加工返回的产品就是hash值

 

Related Posts