资讯

精准传达 • 有效沟通

从品牌网站建设到网络营销策划,从策略到执行的一站式服务

如何理解xml,configpraser和hashlib模块-创新互联

今天就跟大家聊聊有关如何理解xml,configpraser和hashlib模块,可能很多人都不太了解,为了让大家更加了解,小编给大家总结了以下内容,希望大家根据这篇文章可以有所收获。

创新互联于2013年开始,先为信丰等服务建站,信丰等地企业,进行企业商务咨询服务。为信丰企业网站制作PC+手机+微官网三网同步一站式服务解决您的所有建站问题。

写一个XML文件

importxml.etree.ElementTreeasET
namelist=ET.ElementTree("namelist")

将namelist生成一个根节点
name=ET.SubElement(namelist,"name",attrib={"strinf":"yes","name":"alex"})

赋予namelist属性
age=ET.SubElement(name,"age")

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

赋值
role=ET.SubElement(name,"role")

根下建立子节点
role.txt="teacher"et=ET.ElementTree(namelist)

生成文件对象
et.write("test.xml",encoding="utf-8",xml_declaration=True)

写进一个xml文件

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

importconfigparser
config=configparser.ConfigParser()#生成对象
config["DEFAULT"]={'Interval':'45',
                 'Compression':'yes',
                 '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'topsecret['enabled']='YES'config['DEFAULT']['ForwardXll']='yes'f=open("config.ini",'w')
config.write(f)
f.close()

在生成的文件中增删改

importconfigparser
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,'forwardxll')
删除forwardx11这一行

config.set(config_name,'host port','3000')

将端口号改为3000
config.write(open("config2.ini","w"))

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

importhashlib
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(b"alexli")
print(m2.hexdigest())

sha256的比md5的安全

加盐算法,更安全

importhmac
hamc_name=hmac.new(b"salt",b"hello")
print(hamc_name.hexdigest())

看完上述内容,你们对如何理解xml,configpraser和hashlib模块有进一步的了解吗?如果还想了解更多知识或者相关内容,请关注创新互联网站制作公司行业资讯频道,感谢大家的支持。

创新互联www.cdcxhl.cn,专业提供香港、美国云服务器,动态BGP最优骨干路由自动选择,持续稳定高效的网络助力业务部署。公司持有工信部办法的idc、isp许可证, 机房独有T级流量清洗系统配攻击溯源,准确进行流量调度,确保服务器高可用性。佳节活动现已开启,新人活动云服务器买多久送多久。


当前标题:如何理解xml,configpraser和hashlib模块-创新互联
转载注明:http://cdkjz.cn/article/hgsse.html
多年建站经验

多一份参考,总有益处

联系快上网,免费获得专属《策划方案》及报价

咨询相关问题或预约面谈,可以通过以下方式与我们联系

大客户专线   成都:13518219792   座机:028-86922220