How to understand xml,configpraser and hashlib modules
Today, I will talk to you about how to understand xml,configpraser and hashlib module, many people may not know much about it. In order to make you understand better, the editor has summarized the following content for you. I hope you can get something according to this article.
Write a XML file
Import xml.etree.ElementTree as ET
Namelist=ET.ElementTree ("namelist")
Generate the namelist into a root node
Name=ET.SubElement (namelist, "name", attrib= {"strinf": "yes", "name": "alex"})
Assign namelist attributes
Age=ET.SubElement (name, "age")
Create a child node under the root
Age.txt=27
Assignment
Role=ET.SubElement (name, "role")
Establish child nodes under the root
Role.txt= "teacher"
Et=ET.ElementTree (namelist)
Generate file object
Et.write ("test.xml", encoding= "utf-8", xml_declaration=True)
Write to a xml file
Configparser module: can generate and modify configuration files
Import configparser
Config=configparser.ConfigParser () # generate object
Config ["DEFAULT"] = {'Interval':'45'
'Compression':'yes'
'CompressionLevel':'9'}
Assign attributes to generate dictionaries
Config ['bitbucket.org'] = {} can be generated separately
Config ['bitbucket.org'] [' User'] = 'alex'# assignment
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 ()
Add, delete and modify in the generated file
Import configparser
Config=configparser.ConfigParser ()
Config.read ("config1.ini")
Print (config.sections ()) # find the sections of the file
Config_name=config.sections () [1]
# find the corresponding value
Print (config[ config _ name] ["host port"])
Sec=config.remove_option (config_name,'forwardxll')
Delete the forwardx11 line
Config.set (config_name,'host port','3000')
Change the port number to 3000
Config.write (open ("config2.ini", "w"))
Hashlib module: used to verify the consistency of file contents
Import hashlib
M = hashlib.md5 ()
M.update (b "alex")
Print (m.hexdigest ())
M.update (b "li")
Print (m.hexdigest ())
Reading a line of md5 is no more memory-saving than reading the whole md5.
M2=hashlib.md5 ()
M2.update (b "alexli")
Print (m2.hexdigest ())
M2=hashlib.sha256 ()
M2.update (b "alexli")
Print (m2.hexdigest ())
Sha256 is safer than md5.
Salt adding algorithm is more secure.
Import hmac
Hamc_name=hmac.new (b "salt", b "hello")
Print (hamc_name.hexdigest ())
After reading the above, do you have any further understanding of how to understand xml,configpraser and hashlib modules? If you want to know more knowledge or related content, please follow the industry information channel, thank you for your support.