python配置文件读写(ConfigParse)

1. 配置文件(config.ini)

ini文件格式:

[section0] 
key0 = value0
key1 = value1
[section1]
key2 = value2
key3 = value

示例

[config]
NUM = 40
COLUMN = 8
CREATE_SIZE_X = 400
CREATE_SIZE_Y = 300

[color]
# 深蓝
DARK_BLUE =rgba(1,77,103,255)
# 浅蓝
LIGHT_BLUE = rgba(96,143,159,255)
# 亮黄
LUMINOUS_YELLOW = rgba(251,178,23,255)
# 浅黄
LIGHT_YELLOW = rgba(237,222,139,255)
  • 注意ini文件不支持行间注释,可用 ; 或 # 在行首注释

2. 读取配置文件

在python文件中读取config.ini参数

# coding:utf-8
import configparser   # python3以后的安装包改为小写
config = configparser.ConfigParser()
config.read("../config.ini", encoding='utf-8')
print(config.sections())         # 打印所有section
print(config.options("color"))		# 打印[color]这个section里面所有options
print (config.items("color"))		# 打印[color]这个section里面所有键值对
print (config.get("color", "DARK_BLUE"))
print (config.getint("config", "NUM"))
for sections in config.sections():
    for items in config.items(sections):
        #打印单独键值对
        print (items)

打印结果:

['config', 'color', 'db', 'screw', 'mode']
['background_color', 'light_blue', 'purplish_blue', 'dark_blue', 'luminous_yellow', 'light_yellow', 'light_grey', 'grey', 'grey_80', 'grey_60', 'grey_40', 'grey_20', 'grey_10', 'black', 'black_80', 'black_60', 'black_40', 'black_20', 'black_10', 'red', 'red_80', 'red_60', 'red_40', 'red_20', 'red_10']
[('background_color', 'rgba(134,145,153,255)'), ('light_blue', 'rgba(96,143,159,255)'), ('purplish_blue', 'rgba(3,38,58,255)'), ('dark_blue', 'rgba(20,68,106,255)'), ('luminous_yellow', 'rgba(222,125,44,255)'), ('light_yellow', 'rgba(250,218,141,255)'), ('light_grey', 'rgba(179,168,150,255)'), ('grey', 'rgba(134,145,153,255)'), ('grey_80', 'rgba(157,167,175,255)'), ('grey_60', 'rgba(181,198,196,255)'), ('grey_40', 'rgba(205,211,216,255)'), ('grey_20', 'rgba(229,233,235,255)'), ('grey_10', 'rgba(242,244,245,255)'), ('black', 'rgba(0,0,0,255)'), ('black_80', 'rgba(87,87,87,255)'), ('black_60', 'rgba(134,134,133,255)'), ('black_40', 'rgba(176,177,177,255)'), ('black_20', 'rgba(217,217,217,255)'), ('black_10', 'rgba(236,236,236,255)'), ('red', 'rgba(185,0,15,255)'), ('red_80', 'rgba(197,51,63,255)'), ('red_60', 'rgba(211,102,111,255)'), ('red_40', 'rgba(226,153,159,255)'), ('red_20', 'rgba(240,204,207,255)'), ('red_10', 'rgba(248,230,231,255)')]
rgba(20,68,106,255)
40
('num', '40')
('column', '8')
('create_size_x', '400')
('create_size_y', '300')
('background_color', 'rgba(134,145,153,255)')
('light_blue', 'rgba(96,143,159,255)')
('purplish_blue', 'rgba(3,38,58,255)')
('dark_blue', 'rgba(20,68,106,255)')
('luminous_yellow', 'rgba(222,125,44,255)')
('light_yellow', 'rgba(250,218,141,255)')

3. 写入配置文件

#coding:utf-8
import configparser
config = configparser.ConfigParser()
config.read("../config.ini", encoding='utf-8')
if "mode" not in config.sections():
	config.add_section("mode") # 增加一个section
config.set("mode", "flag", "true") # 增加一条数据
with open("../config.ini","w+") as f:
    config.write(f)

配置文件加入结果

[config]
NUM = 40
COLUMN = 8
CREATE_SIZE_X = 400
CREATE_SIZE_Y = 300

[color]
# 深蓝
DARK_BLUE =rgba(1,77,103,255)
# 浅蓝
LIGHT_BLUE = rgba(96,143,159,255)
# 亮黄
LUMINOUS_YELLOW = rgba(251,178,23,255)
# 浅黄
LIGHT_YELLOW = rgba(237,222,139,255)

[mode]
flag = true
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值