1. 程式人生 > >處理xml模塊、configparser模塊、hashlib模塊、subprocess模塊

處理xml模塊、configparser模塊、hashlib模塊、subprocess模塊

direction comm pop item 報錯 ria 三種方式 文檔 三種

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

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)

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())#從管道讀取內容

處理xml模塊、configparser模塊、hashlib模塊、subprocess模塊