1, What is the configparser module?
The configparser module is a python standard library (which means it comes with Python and does not need to be installed). It can be used to operate configuration files with the suffix. ini.
The format of the configuration file parsed by the configparser is similar to that of the. ini configuration file, that is, the file is composed of multiple sections, which cannot be repeated. To find the data in the configuration file, you need to find it through the section.
There are multiple configuration items (options) under each section, which are displayed in the form of key value pairs (items).
2, Data preparation
First, create a new config.ini file (right-click and select new file with the suffix. INI), as follows:
The format of config.ini content is as follows:
[section0] key0 = value0 key1 = value1 [section1] key2 = value2 key3 = value3
The data we prepared are as follows:
# config.ini [CONFIG] # [section0] excel_path = ../test_cases/case_data.xlsx log_path = ../logs/test.log log_level = 1 [email] # [section1] user_name = xxx@qq.com password = 123456
3, Basic use of the configparser module
1. Use the configparser module to read the ini configuration file
In test_ In the configparser.py file, write script code to read the configuration file:
The methods used are: conf.sections(), conf.options(), conf.items(), conf.get()
import configparser # Instantiate the configParser object conf = configparser.ConfigParser() # Read configuration file conf.read("config/config.ini", encoding="utf-8") # Read all sections in the configuration file and return all sections in a list print(conf.sections()) # The execution result is: ['config ',' email '] # Read all key names with section as email in the configuration file print(conf.options("email")) # The execution result is: ['user_name ','password'] # Read all key value pairs with section as email in the configuration file print(conf.items("email")) # The execution result is: [('user_name ',' xxx@qq.com '), ('password', '123456')] # Use the get method to obtain the specific value of the configuration file. Get method: parameter 1 -- > section (section) parameter 2 -- > key (key name) print(conf.get("CONFIG", "excel_path")) # The execution result is:... / test_cases/case_data.xlsx
2. Use the configparser module to write ini configuration files
2.1 write ini configuration file (dictionary form)
Writing in the form of a dictionary can be understood as adding data to the dictionary,
Key: represents the section (string format) in the configuration file;
Value: key value pair of section in dictionary format;
""" 2.1 write in ini Configuration file (dictionary form) """ import configparser # Instantiate the configParser object conf = configparser.ConfigParser() # CONFIG and email are the sections in the configuration file. The contents in the dictionary correspond to the key value pairs under the section conf["CONFIG"] = {"excel_path": "../test_cases/case_data.xlsx", "log_path": "../logs/test.log", "log_level": 1} conf["email"] = {"user_name": "xxx@qq.com", "password": "123456"} # Write the contents of the set conf object to the config.ini configuration file with open("config/config.ini", "w") as configfile: # w. Delete original file and write again conf.write(configfile)
2.2 writing ini configuration file (method form)
Using Add_ When using the section method, if the section already exists in the configuration file, an error will be reported; (the section cannot be repeated, because to find the data in the configuration file, it is through the section, and all must be unique.)
The format is: conf.add_section("section name")
When the set method is used, it is modified if there is one, and new if there is none. The format is:
The format is: conf.set("section", "Name of the key", "The corresponding value of the key")
""" 2.2 write in ini Configuration file (method form) """ import configparser # Instantiate the configParser object conf = configparser.ConfigParser() # Read configuration file conf.read("config/config.ini", encoding="utf-8") # Add a section [webserver] to the conf object (at this time, the contents of the ini file have not been really modified, and the contents of the ini file can be modified only when the conf.write() method is executed) conf.add_section("webserver") # Add the key value pair IP = 127.0.0.1 and port = 8080 in the section [webserver] conf.set("webserver", "ip", "127.0.0.1") conf.set("webserver", "port", "8080") with open("config/config.ini", "w") as f: conf.write(f)
3. Modify the ini configuration file using the configparser module
Use the set() method to modify if any and create if none. The format is:
The format is: conf.set("section", "Name of the key", "The corresponding value of the key")
""" 3,Modify profile """ import configparser # Instantiate the configparser object conf = configparser.ConfigParser() # Read config.ini file conf.read("config/config.ini", encoding="utf-8") # Modify the key in 'CONFIG' to 'log'_ The value of 'path'. If there is no such key, a new one will be created conf.set("CONFIG", "log_path", "../logs/test.log") with open("config.config.ini", "w") as f: conf.write(f)
4. Use the configparser module to delete the ini configuration file
The method used is conf.remove_section() , conf.remove_option()
""" 4,Delete profile content """ import configparser # Instantiate the configparser object conf = configparser.ConfigParser() # Read config.ini file conf.read("config/config.ini", encoding="utf-8") # Delete the specified section conf.remove_section("email") # Deletes the specified key value pair conf.remove_option("CONFIG", "log_level") with open("config/config.ini", "w") as f: conf.write(f)
Summary:
Add used above_ section(), set(), remove_section(),remove_ When using the option () method, the contents of the ini file have not been really modified. The contents of the ini file are modified only when the conf.write() method is executed.