处理xml模块、configparser模块、hashlib模块、subprocess模块

xml模块

新建a.xml内容为:

<data>
    <country name="Liechtenstein">
        <rank updated="yes">2</rank>
        <year updated="yes" version="1.0">2009</year>
        <gdppc>141100</gdppc>
        <neighbor direction="E" name="Austria" />
        <neighbor direction="W" name="Switzerland" />
    <egon age="18">hello</egon><egon age="18">hello</egon></country>
    <country name="Singapore">
        <rank updated="yes">5</rank>
        <year updated="yes" version="1.0">2012</year>
        <gdppc>59900</gdppc>
        <neighbor direction="N" name="Malaysia" />
    <egon age="18">hello</egon><egon age="18">hello</egon></country>
    <country name="Panama">
        <year updated="yes" version="1.0">2012</year>
        <gdppc>13600</gdppc>
        <neighbor direction="W" name="Costa Rica" />
        <neighbor direction="E" name="Colombia" />
    <egon age="18">hello</egon><egon age="18">hello</egon></country>
</data>
 1 import xml.etree.ElementTree as ET
 2 tree = ET.parse('a.xml')#初始化
 3 root = tree.getroot()#获取根节点
 4 for child in root:
 5     print('====>',child)
 6     for i in child:#子节点
 7         print(i.tag,i.attrib,i.text)#tag标签 attrib属性 text内容
 8 
 9 
10 # 查找element元素的三种方式
11 years = root.iter('year')#扫描整个xml文档树(迭代器)
12 for i in years:
13     print(i)
14 
15 print(root.find('country'))#在root的子节点找,只找一个
16 print(root.findall('country'))#在root的子节点找,找所有
17 
18 #修改
19 years = root.iter('year')#扫描整个xml文档树(迭代器)
20 for year in years:
21     year.text = str(int(year.text)+1)
22     year.set('updated','yes')#设置属性
23     year.set('version','1.0')#设置属性
24 tree.write('a.xml')
25 
26 #删除
27 for county in root.iter('country'):
28     rank = county.find('rank')
29     if int(rank.text) >10:
30         county.remove(rank)
31 tree.write('a.xml')
32 
33 
34 #增加
35 for county in root.iter('country'):
36     e = ET.Element('egon')
37     e.text = 'hello'
38     e.attrib = {'age':'18'}#不能为int
39     county.append(e)
40 tree.write('a.xml')

 

configparser模块

新建a.cfg内容为:

[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
#写配置文件 
#更新指定section,option的值 
conf.set("sec_b", "b_key3", "new-$r") 
#写入指定section增加新option和值 
conf.set("sec_b", "b_newkey", "new-value") 
#增加新的section 
conf.add_section('a_new_section') 
conf.set('a_new_section', 'new_key', 'new_value') 
#写回配置文件 
conf.write(open("test.cfg", "w"))

hashlib模块

利用hashlib模块进行加密

import hashlib

m=hashlib.md5()
m.update('hello'.encode('utf-8'))
m.update('world'.encode('utf-8'))#多次update会将字符串内容拼接到一起,进行md5加密
print(m.hexdigest())

# 上面例子相当于
m=hashlib.md5()
m.update('helloworld'.encode('utf-8'))
print(m.hexdigest())

# 更简便方法
m=hashlib.md5('helloworld'.encode('utf-8'))
print(m.hexdigest())

# 也可以写成
m=hashlib.md5('h'.encode('utf-8'))
m.update('elloworld'.encode('utf-8'))
print(m.hexdigest())

# 对文件内容进行hash
m=hashlib.md5()
with open('a.xml','rb') as f:
    for line in f:
        m.update(line)
print(m.hexdigest())

#相当于耗费内存不推荐使用
m=hashlib.md5()
with open('a.xml','rb') as f:
    m.update(f.read())
print(m.hexdigest())

# 加盐:通过md5的特性,在创建m对象时自定义添加一串字符增加加密难度防止被撞库
password='alex3714'
m=hashlib.md5('yihangbailushangqingtian'.encode('utf-8'))
m.update(password.encode('utf-8'))

passwd_md5=m.hexdigest()

print(passwd_md5)

#base64加密
import base64
a = "1qaz@WSX"
b = base64.b64encode(a.encode("utf-8"))

subprocess模块

#subprocess会开启一个子进程来执行命令,不影响整个程序的运行
#语法:
subprocess.Popen('commond',shell=True,stdout=subprocess.PIPE,stderr = subprocess.PIPE)
#shell = true,命令为shell命令,stdout=subprocess.PIPE将正确的结果丢给管道保存stderr=subprocess.PIPE将报错放到管道中
print(res.stdout.read())#从管道读取内容

 

posted @ 2017-08-13 18:43  wateligx  阅读(180)  评论(0编辑  收藏  举报