加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

xml,configpraser,hashlib模块

发布时间:2020-12-16 08:24:12 所属栏目:百科 来源:网络整理
导读:写一个 XML文件 import xml.etree.ElementTree as ET namelist=ET.ElementTree( "namelist" ) 将 namelist生成一个根节点 name=ET.SubElement(namelist, "name" , attrib ={ "strinf" : "yes" ,128);font-size:24px;">"name" : "alex" }) 赋予 namelist属性

写一个XML文件

import xml.etree.ElementTree as ET
namelist=ET.ElementTree("namelist")

namelist生成一个根节点
name=ET.SubElement(namelist,"name",attrib={"strinf":"yes",128);font-size:24px;">"name":"alex"})

赋予namelist属性
age=ET.SubElement(name,128);font-size:24px;">"age")

在根下面建立子节点
age.txt=27

赋值
role=ET.SubElement(name,128);font-size:24px;">"role")

根下建立子节点
role.txt="teacher"

et=ET.ElementTree(namelist)

生成文件对象
et.write("test.xml",153);font-size:24px;">encoding="utf-8",153);font-size:24px;">xml_declaration=True)

写进一个xml文件

configparser模块:可以生成和修改配置文件

import configparser
config=configparser.ConfigParser()#生成对象
config["DEFAULT"]={'Interval':'45',
'Compression':'yes',128);font-size:24px;">'CompressionLevel':'9'}

赋予属性,生成字典
config['bitbucket.org']={}可以单独生成
config['bitbucket.org']['User']='alex'#赋值
config['topsecret.server.com']={}
topsecret=config['topsecret.server.com']
topsecret['Host Port']='50022'
topsecret['ForwardXll']='no''enabled']='YES''DEFAULT']['yes'
f=open("config.ini",128);font-size:24px;">'w')
config.write(f)
f.close()

在生成的文件中增删改

import configparser
config=configparser.ConfigParser()
config.read("config1.ini")
print(config.sections())#找到文件的sections
config_name=config.sections()[1]

#找到对应的值
print(config[config_name]["host port"])

sec=config.remove_option(config_name,128);font-size:24px;">'forwardxll')
删除forwardx11这一行

config.set(config_name,128);font-size:24px;">'host port',128);font-size:24px;">'3000')

将端口号改为3000
config.write("config2.ini",128);font-size:24px;">"w"))

hashlib模块:用来验证文件内容一致性的

import hashlib
m= hashlib.md5()
m.update(b"alex")
print(m.hexdigest())
m.update(b"li")
print(m.hexdigest())

读一行的md5没有比读整篇的md5省内存

m2=hashlib.md5()
m2.update(b"alexli")
print(m2.hexdigest())

m2=hashlib.sha256()
m2.update(print(m2.hexdigest())

sha256的比md5的安全

加盐算法,更安全

import hmac
hamc_name=hmac.new(b"salt",0);font-size:24px;">b"hello")
print(hamc_name.hexdigest())

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读