How Python reads .ini format files (code)

不言
Release: 2018-09-11 15:55:53
Original
11887 people have browsed it

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.

1. ini file format

  • The format is as follows:

; comments
[section1]
Param1 = value1
Param2= value2
[section2]
Param3= value3
Param4= value4
Copy after login
  • [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.

2. Read the ini file

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'))
Copy after login

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')]
Copy after login

3. Modify and write the ini file

  • 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+'))
Copy after login

related recommendations:

A very perfect PHP configuration class sharing for reading and writing ini format, reading and writing ini format php

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!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template