PYTHON_WAY.DAY71, module
Configparser,xml,shutil,subprocess
2. Object-oriented (top)
The creation of classes, the composition of classes, the selection of functional programming and object-oriented programming, the inheritance of classes
1. Module Configparser
Used to manipulate files in a particular format, using open on their own, only [test1]-specific formats
= 123== 123= v1
File Contents
1. Get
ImportConfigparser#Open the file to find all the item tags under the fileConfig = Configparser. Configparser ()#Create a Configparser objectConfig.read ("file1", encoding="Utf-8")#to read a file intoret = Config.sections ()#Find all the tagsPrint(ret) ['test1','test2']#obtain the corresponding key & velvet under the Test1 nodeRet1 = Config.items ("test1")Print(RET1) [('K1','123'), ('K2','v1')]#get all keys under the Test1 nodeRet2 = Config.options ("test1")Print(Ret2) ['K1','K2']#get all the Velue under the nodeRet3 = Config.get ("test1","K1")#Get StringPrint(Ret3,type (ret3))123 <class 'Str'>The following obtained data of these types must first be converted to the corresponding type in order to convert Ret3= Config.getint ("test1","K1")#Get integer typePrint(Ret3,type (ret3))123 <class 'int'>Ret3= Config.getfloat ("test1","K1")#get floating point typePrint(Ret3,type (ret3))123.0 <class 'float'>Ret3= Config.getboolean ("test1","K2")#Gets the Boolean valuePrint(Ret3,type (RET3)) True <class 'BOOL'>
2. Check, delete, add nodes
ImportConfigparserconfig=Configparser.configparser () config.read ("file1", encoding="Utf-8")#CheckHas_ses = Config.has_section ("test1")Print(has_ses) True#Add a node--if an error is already present in the nodes nameConfig.add_section ("test3") config.write (open ("file1","W"))#Delete node--if no this node does not errorConfig.remove_section ("test3") config.write (open ("file1","W"))
3. Check, delete, set the key value pair in the specified group
Import Configparser
Config.configparser.configParser ()
Config.read ("File1", encoding= "Utf-8")
Check
Config.has_option ("Test1", "K1") #检查test1下有没有k1这个键
Delete
Config.remove_option ("Test1", "K1") #删除指定节点下的k1
Config.write ("Open (File1", "W")) #将删除后的结果存入文件, delete the file automatically after
Set up
Config.set ("Test2", "K1", "234") #将test2中的 K1 and 234 are saved to the configuration file, if there is no error.
Config.write (Open ("File1"), "W")
Python_way.day7 Module (configparser,xml,shutil,subprocess), object-oriented (top) (Create class, class composition, functional programming and object-oriented programming selection, class inheritance)