This article is participating in Python Theme Month. See the link to the event for more details

background

Configuration files are often used in real life. Reading and writing configuration files is a basic operation. Today we will learn how to easily read and write configuration files in Python.

About the configparser

Configparser is Python’s built-in library for parsing configuration files. It can read and write ini files and parse placeholders in configuration files.

How to use the configparser

Creating a Configuration file

Create a configuration file named info.ini with the following contents:

[base]
name = phyger
age = 18

[nova]
port = 9292
user = nova
pwd = t@123
Copy the code

Above, the character with square brackets is named section, the configuration block. A character in the format of K = V is named Option, which is a configuration item.

Reading configuration

The configuration file and code are in the same path, as follows:

import configparser

config=configparser.ConfigParser()

config.read('info.ini')
print(config)
Copy the code

The output is as follows:

<configparser.ConfigParser object at 0x000001CCBC3B8AC8>
Copy the code

As above, config is a ConfigParser object.

Read the section

import configparser

config=configparser.ConfigParser()

config.read('info.ini')
print(config['base'])
Copy the code

The output is as follows:

<Section: base>
Copy the code

As above, the result is a Section object named base.

Read the option 1

import configparser

config=configparser.ConfigParser()

config.read('info.ini')
print(config['base'] ['name'])
Copy the code

The output is as follows:

phyger
Copy the code

As shown above, the value of name in the base configuration block is phyger in the configuration file.

Read option – 2

import configparser

config=configparser.ConfigParser()

config.read('info.ini')
print(config.get('base'.'name'))
Copy the code

The results obtained by the GET method and the above slice method are consistent.

Read the option list in the section

import configparser

config=configparser.ConfigParser()

config.read('info.ini')
print(config.items('base'))
Copy the code

The output is as follows:

[('name', 'phyger'), ('age', '18')]
Copy the code

The application of interpolation

Modify the configuration file as follows:

[base]
name = phyger
age = 18
msg = %(name)s-%(age)s

[nova]
port = 9292
user = nova
pwd = t@123
Copy the code

Code:

import configparser

config=configparser.ConfigParser()

config.read('info.ini')
print(config.get('base','msg'))
Copy the code

The output is as follows:

phyger-18
Copy the code

Configuration to

Requirement: Add a section named hello, add an option.

import configparser

# Create ConfigParser object
config=configparser.ConfigParser()

Read the configuration file
config.read('info.ini')
print(config.get('base'.'msg'))

# to add a section
config.add_section('hello')

Add option #
config['hello'] ['key'] ='value'

# Write config object to config file
with open('info.ini',mode='w') as fp:
    config.write(fp)
Copy the code

Configuration file after writing:

[base]
name = phyger
age = 18
msg = %(name)s-%(age)s

[nova]
port = 9292
user = nova
pwd = t@123

[hello]
key = value
Copy the code

In actual combat

Options are changed while the code is running, so that subsequent code can use the new option.

import configparser

# Create ConfigParser object
config=configparser.ConfigParser()

Read the configuration file
config.read('info.ini')
name1=config.get('base'.'name')
print('on start, my name is',name1)

# change option
config['base'] ['name'] ='flyboy'

# Write config object to config file
with open('info.ini',mode='w') as fp:
    config.write(fp)

# Select * from 'py' where 'py' is a configuration file and 'py' is a configuration file.
name2=config.get('base'.'name')

print('after modified, my name is',name2)
Copy the code

The output is as follows:

on start, my name is phyger
after modified, my name is flyboy
Copy the code