Python Study Notes -2017.5.4thon study notes-2017.8.11

Source: Internet
Author: User

JSON pickle serialization can dump multiple times, but cannot load multiple problems we can use the Shelve Module #shelve module, is the pickle more layer of the package import shelve,datetimed = Shelve.open (" Shelve module ") A = {" name ":" Jack "," Job ":" It "}b = [1,2,3,4,5,6]c = (7,8,9,datetime.datetime.now ()) d[" a "] = ad[" B "] = bd[" C "] = Cd.close () Print (D.get ("a")) Print (D.get ("B")) Print (D.get ("C")) # Print (D.items ()) E = D.items () for I in E:print (i) # XmlImport Xml.etree.ElementTree as Ettree = Et.parse ("National Provinces and Cities. xml") root = Tree.getroot () #取得根目录print (root) for child in Root: #取得子title print (Child.tag, child.attrib) #子title属性 for I in Child:print (I.tag, I.text) #子title字符 # As above is traversing all and playing Print, the following example only loops with a field and prints for the Root.iter ("year"): Print (Node.tag,node.attrib, node.text) #如果有很多层, only one layer at a to continue to traverse down, #    Modify the XML file for node in Root.iter ("year"): Print (Node.tag,node.attrib, node.text) newyear = Int (node.text) + # to int and add 1 Node.text = str (newyear) #重新修改为字符串格式并赋值给year node.set ("Update", "Yes") #加入新的属性tree. Write ("National Provinces and Cities 2.xml") #也可以写入原文件名字, Write directly to the original file. #删除for Country in Root.findall ("Country"): #找To all country nodes rank = Int (Country.find ("Rank"). Text) if rank > 50:root.remove ("Cuntry") tree.write ("xxxxx.x ML ") #写入 # Create an XML document yourself import Xml.etree.ElementTree as Etnew_xml = ET. Element ("NameList") #根节点name = ET. Subelement (New_xml, "name", attrib={"Jack": "Yes"}) #newxml子节点age = ET. subelement (Name, "Age", attrib={"Jack": "*"}) #name子节点sex = ET. subelement (name, "Sex") #name子节点sex. Text = "Male" #赋值et = ET. ElementTree (New_xml) #生成文档对象et. Write ("Test.xml", encoding= "Utf-8", Xml_declaration=true #xml =1.0) #写入文件ET. Dump (new_ XML) #生成 #pyyaml, write the configuration document using #configparser, another write configuration file __author__ = "Jack" Import configparserconfig = Configparser.                    Configparser () #生成一个config处理对象config ["DEFAULT"] = {"Serveraliveinterval": "Compression": "Yes", "CompressionLevel": 9} #生成对象并填写类似于字典的key和valueconfig ["bitbucket.org"] = {} #先生成对象config ["bitbucket.org"] [User] ] = "HG" #再填写keyconfig ["topsecret.server.com"] ={}topsecrect = config["topsecret.server.com"]topsecrect["Host Port"] = "50022" topsecrect["ForwardX11"] = "no" config["DEFAULT" ["ForwardX11"] = "yes" with open ("Exzample.ini", "W") as Configfile:config. Write (ConfigFile) will generate the following INI configuration file [default]compression = Yescompressionlevel = 9serveraliveinterval = 45forwardx11 = yes[ Bitbucket.org]user = Hg[topsecret.server.com]host Port = 50022forwardx11 = no# As above is to write import Configparserconfig = Configparser. Configparser () config.sections () #config. Read ("Exzample.ini") print (Config.sections ()) #不打印defaultprint ( Config.defaults ()) #打印default, which means printing all print (config["bitbucket.org" ["User"]) #删除print (Config.has_section (" Bitbucket.org ")) #是否存在bitbucket节点sec = Config.remove_section (" bitbucket.org ") config.write (Open (" Test1 "," W ")) # Delete this node and re-write a new Test1 file

Python Study Notes -2017.5.4thon study notes-2017.8.11

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.