首页 > 编程语言 > 详细

python_day06 常用模块xml/configparser/hashlib/subprocess 面向对象程序设计

时间:2017-08-14 09:28:53      阅读:224      评论:0      收藏:0      [点我收藏+]

常用模块
shutil
xml
configparser
hashlib
suprocess
面向对象的程序设计

 

常用模块

xml模块

技术分享
 1 <?xml version="1.0"?>
 2 <data>
 3     <country name="Liechtenstein">
 4         <rank updated="yes">2</rank>
 5         <year>2008</year>
 6         <gdppc>141100</gdppc>
 7         <neighbor name="Austria" direction="E"/>
 8         <neighbor name="Switzerland" direction="W"/>
 9     </country>
10     <country name="Singapore">
11         <rank updated="yes">5</rank>
12         <year>2011</year>
13         <gdppc>59900</gdppc>
14         <neighbor name="Malaysia" direction="N"/>
15     </country>
16     <country name="Panama">
17         <rank updated="yes">69</rank>
18         <year>2011</year>
19         <gdppc>13600</gdppc>
20         <neighbor name="Costa Rica" direction="W"/>
21         <neighbor name="Colombia" direction="E"/>
22     </country>
23 </data>
xml数据(a.xml)
import xml.etree.ElementTree as ET
tree=ET.parse(‘a.xml‘)
root=tree.getroot()
for child in root:
    print(‘====>‘,child.tag)
    for i in child:
        print(i.tag,i.attrib,i.text)
关注:标签名、属性、文本内容

#查找element元素的三种方式
years=root.iter(‘year‘) #扫描整个xml文档树,找到所有
for i in years:
    print(i)

res1=root.find(‘country‘) #谁来调,就从谁下一层开始找,只找一个
print(res1)
res2=root.findall(‘country‘) #谁来调,就从谁下一层开始找,只找一个
print(res2)

#修改子节点
years=root.iter(‘year‘) #扫描整个xml文档树,找到所有
for year in years:
    year.text=str(int(year.text)+1)
    year.set(‘updated‘,‘yes‘)
    year.set(‘version‘,‘1.0‘)
tree.write(‘a.xml‘)

#删除
for county in root.iter(‘country‘):
    # print(county.tag)
    rank=county.find(‘rank‘)
    if int(rank.text) > 10:
        county.remove(rank)
tree.write(‘a.xml‘)

#增加节点
for county in root.iter(‘country‘):
    e=ET.Element(‘egon‘)
    e.text=‘hello‘
    e.attrib={‘age‘:‘18‘}
    county.append(e)

#前面增删改操作一定要写入到a.xml才会生效
xmltree.write(‘a.xml‘)

configparser

技术分享
[egon]
name = egon
is_admin = False
salary = 3.1

[alex]
name = alex
is_admin = True
a.ini
import configparser
config=configparser.ConfigParser()
config.read(‘a.ini‘)
#取配置
print(config.sections()) #看标题
print(config.options(config.sections()[0])) #查看某个标题下的配置项
res=config.get(‘alex‘,‘name‘)#查看某个标题下的某个配置项的值
print(type(res))
# getint/getfloat/getboolean
res1=config.getfloat(‘egon‘,‘salary‘)#查看某个标题下的某个配置项的值
print(type(res1))
res1=config.getboolean(‘egon‘,‘is_admin‘)#查看某个标题下的某个配置项的值
print(type(res1))
#修改
config.remove_section(‘alex‘)
config.remove_option(‘egon‘,‘age‘)
#添加
config.add_section(‘alex‘)
config.set(‘alex‘,‘name‘,‘SB‘)
#写入文件
config.write(open(‘a.ini‘,‘w‘))

hashlib

三个特点:
1.内容相同则hash运算结果相同,内容稍微改变则hash值则变
2.不可逆推
3.相同算法:无论校验多长的数据,得到的哈希值长度固定。

import hashlib
#如下几种加密方式结果一样
m=hashlib.md5()
m.update(‘hello‘.encode(‘utf-8‘))
m.update(‘world‘.encode(‘utf-8‘))
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())

#加密文件
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())

#加盐
password=‘alex3714‘
m=hashlib.md5(‘yihangbailushangqingtian‘.encode(‘utf-8‘))
m.update(password.encode(‘utf-8‘))
passwd_md5=m.hexdigest()
print(passwd_md5)

#hmac模块
#默认第一次加密就会加盐
import hmac
h=hmac.new(‘hello‘.encode(‘utf-8‘))
h.update(‘world‘.encode(‘utf-8‘))
print(h.hexdigest())
#0e2564b7e100f034341ea477c23f283b
h=hmac.new(‘hel‘.encode(‘utf-8‘))
h.update(‘loworld‘.encode(‘utf-8‘))
print(h.hexdigest())
#2b8861887b9670e3b042475182619b5d
h=hmac.new(‘hello‘.encode(‘utf-8‘))
h.update(‘w‘.encode(‘utf-8‘))
h.update(‘or‘.encode(‘utf-8‘))
h.update(‘ld‘.encode(‘utf-8‘))
print(h.hexdigest())
#0e2564b7e100f034341ea477c23f283b

suprocess

import subprocess
res=subprocess.Popen(r‘deeddddir D:\04-视频录制存放目录\python18期\day7\xml模块‘,
                     shell=True,
                     stdout=subprocess.PIPE,
                     stderr=subprocess.PIPE)
print(‘=================>‘) #程序会先打印此处
print(‘=================>‘,res)
print(‘-========>‘,res.stdout.read())
print(‘-========>‘,res.stderr.read().decode(‘gbk‘))     #windows为gbk解码,Linux为ut-8解码
print(‘-========>‘,res.stderr.read().decode(‘gbk‘))     #管道信息只获取一次,之后再获取就没有了
#dir file_path | findstr xml$
res1=subprocess.Popen(r‘dir E:\获取的资料\老男孩教育-Python自动化课程-20170701\day07\day7纯代码\xml模块‘,
                     shell=True,
                     stdout=subprocess.PIPE,)
stdin=res1.stdout    #输出作为下一次的输入
res2=subprocess.Popen(r‘findstr xml$‘,
                     shell=True,
                     stdin=res1.stdout,
                     stdout=subprocess.PIPE,)
print(res2.stdout.read().decode(‘gbk‘))

面向对象的程序设计

 

python_day06 常用模块xml/configparser/hashlib/subprocess 面向对象程序设计

原文:http://www.cnblogs.com/liweijing/p/7355929.html

踩
(0)
赞
(0)
   
举报
评论 一句话评论(0)
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!