Module URLLIB requests JSON XML Configparser learning notes

Source: Internet
Author: User

Initiating an HTTP request gets the return value of the return value is a string

Third-party module install PIP install requests

return value format XML HTML Jaon

Json

function loads string The list dictionary string must be "", because the other programming language strings are ""

Dups List "" String

Xml

From Xml.etree import elementtree as Ettree = Et.parse (' 1.xml ') root = Tree.getroot () print (type root) /c1>
# <class ' xml.etree.ElementTree.Element ' >
# dir (Element):
# append ', ' attrib ', ' clear ', ' extend ', ' find ', ' findall ', ' findtext ', ' get ', ' getchildren ', ' getiterator ', ' Insert ', ' ite Ms ', ' iter ', ' iterfind ', ' itertext ', ' Keys ', ' makeelement ', ' Remove ', ' set ', ' tag ', ' tail ', ' text '

<class ' Xml.etree.ElementTree.Element ' >

Dir (Element):

Append ', ' attrib ', ' clear ', ' extend ', ' find ', ' findall ', ' findtext ', ' get ', ' getchildren ', ' getiterator ', ' Insert ', ' item S ', ' iter ', ' iterfind ', ' itertext ', ' Keys ', ' makeelement ', ' Remove ', ' set ', ' tag ', ' tail ', ' text '

From Xml.etree import elementtree as Ettree = Et.parse (' 1.xml ') 
#ET. Parse (add file path)  ET. Element (file node, element object)
root = Tree.getroot () son = root.makeelement ("Dat", {"K1": "K2"}) Root.append (son) s = son.makeelement ("tt", {"KS" : "vs"}) Son.append (s) tree.write ("1.xml")
#添加子节点 grandson Node

Add child nodes grandchild node

From Xml.etree import elementtree as Ettree = Et.parse (' 1.xml ') root = Tree.getroot () son = ET. Element ("Dat", {"K1": "K2"}) Root.append (son) tree.write ("1.xml")
#直接根据类Element创建节点

To create a node directly from a class element

From Xml.etree import elementtree as Ettree = Et.parse (' 1.xml ') print(Tree,type (tree)) from Xml.etree.ElementTree Import ElementTree  #用pychrm查找类ElementTree
#1   method Paese is to call the ElementTree class to create the object
#2 method Getroot get XML with node
#3 Method Write writes the child nodes that are created in memory to the file

#创建XML文档1from xml.etree import elementtree as Etroot = ET. Element ("Famliy") Son1 = ET. Element (' son ', {' name ': ' Er1 '}) Son2 = ET. Element (' son ', {' name ': ' Er2 '}) root.append (Son1) root.append (son2) sun1 = ET. Element (' Sun ', {' name ': ' s1 '}) sun2 = ET. Element (' Sun ', {' name ': ' S2 '}) son1.append (SUN1) son2.append (sun2) tree = ET. ElementTree (Root) tree.write (' 1.xml ')      
# Create XML document 2from Xml.etree import elementtree as Etroot = ET. Element (' famliy ') Son1 = ET. subelement (Root, ' sun ', attrib={' name ': "S1"}) Son2 = ET. subelement (Root, ' sun ', attrib={' name ': "S2"}) Root.append (Son1) root.append (son2) tree = ET. ElementTree (Root) tree.write (' 2.xml ', encoding= ' utf-8 ')    
# Create an XML document 3from Xml.etree import elementtree as et# create a root node, root = ET. Element ("Famliy") # creates the big son # son1 = ET. Element (' son ', {' name ': ' Child 1 '}) Son1 = Root.makeelement (' son ', {' name ': ' Child 1 '}) # Create the youngest son # son2 = ET. Element (' son ', {' name ': ' Child 2 '}) Son2 = Root.makeelement (' son ', {"name": ' Child 2 '}) # Create two grandchildren in the older son # grandson1 = ET. Element (' grandson ', {' name ': ' Child One '}) Grandson1 = Son1.makeelement (' grandson ', {' name ': ' Child One '}) # grandson2 = ET. Element (' grandson ', {' name ': ' Child '}) Grandson2 = Son1.makeelement (' grandson ', {' name ': ' Child '}) Son1.append ( Grandson1) Son1.append (grandson2) # Add a son to the root node root.append (Son1) root.append (son1) tree = ET. ElementTree (Root) tree.write (' Oooo.xml ', encoding= ' utf-8 ', short_empty_elements=False) Creation Method (ii)   

#带缩进的创建xml from Xml.etree import elementtree as Etroot = ET. Element (' famliy ') Son1 = ET. subelement (Root, ' sun ', attrib={' name ': "S1"}) Son2 = ET. subelement (Root, ' sun ', attrib={' name ': "S2"}) Root.append (Son1) root.append (son2) def Mywrite (root,name): From    xml.dom import minidom rough_string = et.tostring (Root, ' utf-8 ') reparsed = minidom.parsestring ( rough_string) New_str = reparsed.toprettyxml (indent= "\ T") F = open (name, ' W ', encoding= ' Utf-8 ') f.write (new_ STR) f.close () Mywrite (Root, "5.xml")         

Configparser for working with files in a specific format

Specific format # note 1;  Note 2 [Section1] # node K1 = v1    # value K2:v2       # value [Section2] # node K1 = v1    # value 
Import Configparsercon = configparser. Configparser () con.read (' in ', encoding= ' utf-8 ') result = con.sections () print(result) ret = Con.options (' Section1 ') print (ret)     

Module URLLIB requests JSON XML Configparser learning notes

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.