8.2 introduction to common modules 2: xml, configparser, hashlib,

Source: Internet
Author: User

8.2 introduction to common modules 2: xml, configparser, hashlib,
Xml:

Introduction: Contains functions related to the extensible markup language xml

Usage:

  • Python has three ways to parse XML--SAX, DOM, and ElementTree, Because xml technology lags behind, so here is not the introduction of sax, dom:

 

Xml text:

<?xml version="1.0"?><data>    <fruit name="apple">        <color>red</color>    </fruit>    <fruit name="banana">        <color>yellow</color>    </fruit>    <fruit name="pine">        <color>unknow</color>    </fruit></data>

Use of ElementTree: import module -- get resolution object -- get node -- operation Node

 

 

# Import xml. etree. elementTree as ET # create the corresponding parsing Object tree = ET. parse ('data. xml ') # obtain the node root = tree. getroot () # Get the root node print (root. tag) # print (root. getchildren () # obtain the child node through iteration. We recommend that you use "for child in root: print (child. tag) # obtain the label print (child. attrib) # obtain the attributes of the child node print (child. text) # Get the text of the child node for cc in child: # Sun Tzu node print (cc. tag) print (cc. attrib) print (cc. text) print ("-----------------") # search for a node by Tag Name: for I in root. findall ('fruit'): # This can only be found at the subnode print (I. tag) print (I. attrib) print (I. text) print ("**********************") for I in root. iter ('color'): print (I. tag) print (I. text) ### search for a node and modify print ("***********************") for I in root. iter ('color'): print (I. text) if I. text = 'unknow': I. text = 'unknowd '# Modify text # Use set: I. set (attribute name, attribute value) tree. write ("data2.xml") # It can be written back to the original file or a new file ### search for a node and delete it for I in root. findall ('fruit'): if I. attrib ['name'] = 'pine': root. remove (I) # Delete the child tree from the parent node. write ("data3.xml") # It can be written back to the original file or a new file ### create a node new_xml = ET. element ("new parent") # create a parent node Note: Only one parent node exists, which is equivalent to creating xmlson_name = ET. subElement (new_xml, "name", attrib = {"att": "yes"}) # create a subnode son_color = ET. subElement (son_name, "color", attrib = {"att": "no"}) son_color.text = 'unknow' name2 = ET. subElement (new_xml, "name", attrib = {"att": "no"}) color2 = ET. subElement (name2, "color") et = ET. elementTree (new_xml) # generate the Document Object et. write ("test. xml ", encoding =" UTF-8 ", xml_declaration = True) # Write the file and determine the format.

 

Configparser:

Introduction: including functions related to configuration files

Initial text content:

[DEFAULT]ip = 192.168.1.1hostname=aotuman[african]color = blackluck = no

Usage:

# Import module import configparser # create a parser con = configparser. configParser () print ("------- read -------------") con. read ('config. ini ') print (con. default_section) # print (con. defaults () # default label result print (con. sections () # print the name of a tag other than the default tag (con ['African '] ['color']) # obtain the corresponding tag attribute print ("------------- write method ------------") con ["DEFAULT"] = {'root': 'yes'} # override con ['Europe'] = {'color': 'white', 'luck ': 'super'} # con ['Europe'] ['LIFE'] = '0' # add style modify with open ('example. ini ', 'w') as configfile: con. write (configfile) # write back, or write to the new file print ("------------- add ----------") # first determine whether there is if con. has_section ('superman ') = False: con. add_section ('superman ') con ['superman'] = {'SIZE': 'Big '} with open ('example2. ini ', 'w') as configfile: con. write (configfile) # write back, or write to the new file print ("------------- Delete ----------") # con. remove_option ('Europe', 'LIFE') # Delete the con. remove_section ('Europe') # delete a tag with open ('example3. ini ', 'w') as configfile: con. write (configfile) # write back or write to a new file

 

Note: All values must use strings.

 

Hashlib:

Introduction: includes hash encryption functions [hmac and hashlib are common encryption modules in python]

Usage:

# Import module import hashlibmy_md5 = hashlib. md5 () # select the encryption method and create the object my_md5.update (B '000000') # Add the encrypted text my_md5.update ('Chinese '. encode () # convert to bytes to encrypt print (my_md5.hexdigest () # display encrypted information in hexadecimal format (commonly used) ######### sha1 ####### hash = hashlib. sha1 () hash. update (B '123') print (hash. hexdigest () ######### sha256 ######## hash = hashlib. sha256 () hash. update (B '123') print (hash. hexdigest () ######### sha384 ####### hash = hashlib. sha384 () hash. update (B '123') print (hash. hexdigest () ######### sha512 ######## hash = hashlib. sha512 () hash. update (B '123') print (hash. hexdigest ())

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.