python库配置文件ini之configparser


描述

  • 第三方库:
pip install configparser
  • 配置文件example.ini格式,比如
[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()

# 直接初始化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)

不包含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

包含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

符号:引号、等号、%

  • 引号:不用特意将字符串带上引号
  • 等号:值中有=,就直接写就行。
  • %:如果你不想插值的话,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
'''

Reference

https://pypi.org/project/configparser/
https://docs.python.org/3/library/configparser.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值