1. 描述

About 2 min


1. 描述

  • 第三方库:
pip install configparser
  • 配置文件example.ini格式,比如
[DEFAULT]
serveraliveinterval = 45
compression = yes
compressionlevel = 9

[bitbucket.org]
user = hg

[topsecret.server.com]
port = 50022
forwardx11 = no

2. 写

# -*- coding: utf-8 -*-

import configparser

config = configparser.ConfigParser()

# 直接初始化section
config['DEFAULT'] = {'ServerAliveInterval': '45','Compression': 'yes','CompressionLevel': '9'}

# 先创建section,才能再写入键值对
config['bitbucket.org'] = {}
config['bitbucket.org']['User'] = 'hg'

# 获取section对象后写键值对
config['topsecret.server.com'] = {}
topsecret = config['topsecret.server.com']
topsecret['Port'] = '50022'     # mutates the parser
topsecret['ForwardX11'] = 'no'  # same here

# 写入到文件中
with open('example.ini', 'w') as configfile:
    config.write(configfile)

3. 读

3.1. 不包含DEFAULT

[bitbucket.org]
user = hg

[topsecret.server.com]
port = 50022
forwardx11 = no
# -*- coding: utf-8 -*-

import configparser

config = configparser.ConfigParser()
config.read("example.ini")

# 列出section
print(config.sections())
# ['bitbucket.org', 'topsecret.server.com']

# 检测section
print('bitbucket.org' in config)
# True

# 列出键
print(config.options("bitbucket.org"))
# ['user']

# 列出键
# print(config['bitbucket.org'])	# <Section: bitbucket.org>
for key in config['bitbucket.org']:  
    print(key,end=' ')
print()
# user

# 读取键值对
print(config["bitbucket.org"]["user"])
# hg

# 用读取到的section读取键值对
bitbucket = config["bitbucket.org"]
user = bitbucket["user"]
print(user)
# hg

3.2. 包含DEFAULT

命名为DEFAULT的section是特殊的:

  • 会被config.sections()跳过,但检测section还是能检测到的
  • 其他的section会包含DEFAULTsection中的内容
[DEFAULT]
serveraliveinterval = 45
compression = yes
compressionlevel = 9

[bitbucket.org]
user = hg

[topsecret.server.com]
port = 50022
forwardx11 = no
# -*- coding: utf-8 -*-

import configparser

config = configparser.ConfigParser()
config.read("example.ini")

# 列出section
print(config.sections())
# ['bitbucket.org', 'topsecret.server.com']

# 检测section
print('DEFAULT' in config)
# True

# 列出键
print(config.options("bitbucket.org"))
# ['user', 'serveraliveinterval', 'compression', 'compressionlevel']

# 列出键
print(config['bitbucket.org'])
    print(key,end=' ')
print()
# user serveraliveinterval compression compressionlevel

# 读取键值对
print(config["bitbucket.org"]["serveraliveinterval"])
# 45

# 用读取到的section读取键值对
bitbucket = config["bitbucket.org"]
serveraliveinterval = bitbucket["serveraliveinterval"]
print(serveraliveinterval)
# 45

4. 符号:引号、等号、%

  • 引号:不用特意将字符串带上引号
  • 等号:值中有=,就直接写就行。
  • %:如果你不想插值的话,config = configparser.ConfigParser(interpolation=None),不要使用RawConfigParser
[words]
w1 = 1+1=2
w2 = '1+1=2'
w3 = 1+1\=2
w4 = %45%56
# -*- coding: utf-8 -*-

import configparser

config = configparser.ConfigParser(interpolation=None)
config.read("example.ini")

# 读取键值对
for key in config["words"]:
    print(config["words"][key])

'''
1+1=2
'1+1=2'
1+1\=2
%45%56
'''

5. Reference

https://pypi.org/project/configparser/open in new windowhttps://docs.python.org/3/library/configparser.htmlopen in new window