Home > Article > Backend Development > How Python reads .ini format files (code)
The content of this article is about how Python reads .ini format files (code). It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.
Everyone should have come into contact with configuration files in .ini format. The configuration file extracts some configuration-related information and manages it separately. If there are changes in the future, you only need to change the configuration file without modifying the code. Especially for subsequent automated testing, part of the configuration information needs to be extracted and managed. For example, the mailbox configuration information for sending emails, database connection and other information.
Today I will introduce how to use Python to read the ini configuration file.
The format is as follows:
; comments [section1] Param1 = value1 Param2= value2 [section2] Param3= value3 Param4= value4
[section]
: The section module of ini is a general name for the following parameter values, which is convenient and easy to remember.
Param = value
: Parameter and parameter value.
In the ini file, use ";" for comments.
Python has its own module ConfigParser for reading configuration files. The configuration file is not case-sensitive.
There are a series of methods available.
read(filename)
: Read the file content
sections()
: Get All sections are returned as a list.
options(section)
: Get all options of this section.
items(section)
: Get all key-value pairs of this section.
get(section,option)
: Get the value of option in section and return the string type.
getint(section,option)
: Get the value of option in section and return the int type.
For example:
import os import configparser # 当前文件路径 proDir = os.path.split(os.path.realpath(__file__))[0] # 在当前文件路径下查找.ini文件 configPath = os.path.join(proDir, "config.ini") print(configPath) conf = configparser.ConfigParser() # 读取.ini文件 conf.read(configPath) # get()函数读取section里的参数值 name = conf.get("section1","name") print(name) print(conf.sections()) print(conf.options('section1')) print(conf.items('section1'))
Run result:
D:\Python_project\python_learning\config.ini 2号 ['section1', 'section2', 'section3', 'section_test_1'] ['name', 'sex', 'option_plus'] [('name', '2号'), ('sex', 'female'), ('option_plus', 'value')]
write(fp)
: Write the config object to an ini format file.
add_section(section)
: Add a new section.
set(section, option, value)
: To set the option in the section, you need to call write to write the content into the configuration file.
remove_section(section)
: Delete a section.
remove_option(section,option)
: Delete the option under a certain section
For example: continue Part
# 写入配置文件 set() # 修改指定的section的参数值 conf.set("section1",'name','3号') # 增加指定section的option conf.set("section1","option_plus","value") name = conf.get("section1","name") print(name) conf.write(open(configPath,'w+')) # 增加section conf.add_section("section_test_1") conf.set("section_test_1","name","test_1") conf.write(open(configPath,'w+'))
related recommendations:
Example of python reading and writing ini files (python reading and writing files)
The above is the detailed content of How Python reads .ini format files (code). For more information, please follow other related articles on the PHP Chinese website!