python解析ini配置文件之configparser

帮助文档

configparser — Configuration file parser — Python 3.12.1 documentation

示例代码:

import configparser

config = configparser.ConfigParser()
config['database_product'] = {
    "host": "127.0.0.1",
    "user": "mysql",
    "password": "123456",
    "database": "test",
    "charset": "utf8"
}

config['database_develop'] = {
    "host": "127.0.0.1",
    "user": "root",
    "password": "123456",
    "database": "test",
    "charset": "utf8"
}

config['test'] = {}

test = config['test']
test['ip'] = "1.1.1.1"
test['port'] = "3306"

with open('../etc/config.ini', 'w') as configfile:
    config.write(configfile)

生成文件: ../etc/config.ini

[database_product]
host = 127.0.0.1
user = mysql
password = 123456
database = test
charset = utf8

[database_develop]
host = 127.0.0.1
user = root
password = 123456
database = test
charset = utf8

[test]
ip = 1.1.1.1
port = 3306

# 执行前配置文件内容
[database_product]
host = 127.0.0.1
user = mysql
password = 123456
database = test
charset = utf8

[database_develop]
host = 127.0.0.1
user = root
password = 123456
database = test
charset = utf8

[test]
ip = 1.1.1.1
port = 3306


# python示例代码
import configparser

config = configparser.ConfigParser()

# 从文件读取配置内容
config.read('../etc/config.ini')
# 删除配置项
config.remove_section('test')
# 删除配置项中的内容
config.remove_option('database_develop', 'charset')

# 将修改后的内容写入配置文件
with open('../etc/config.ini', 'w') as configfile:
    config.write(configfile)


# 执行后配置文件内容
[database_product]
host = 127.0.0.1
user = mysql
password = 123456
database = test
charset = utf8

[database_develop]
host = 127.0.0.1
user = root
password = 123456
database = test

# 更新前配置文件
[database_product]
host = 127.0.0.1
user = mysql
password = 123456
database = test
charset = utf8

[database_develop]
host = 127.0.0.1
user = root
password = 123456
database = test


# 示例代码
import configparser

config = configparser.ConfigParser()
# 读取配置文件内容
config.read('../etc/config.ini')
# 设置参数值
config['database_develop']['database'] = 'test111'
# 设置参数值
config.set('database_develop', 'user', 'mysql')
# 将修改后的配置写入文件
with open('../etc/config.ini', 'w') as configfile:
    config.write(configfile)


# 更新后配置文件
[database_product]
host = 127.0.0.1
user = mysql
password = 123456
database = test
charset = utf8

[database_develop]
host = 127.0.0.1
user = mysql
password = 123456
database = test111

# 配置文件内容
[database_product]
host = 127.0.0.1
port = 3306
user = mysql
password = 123456
database = test
charset = utf8
is_mysql = True

[database_develop]
host = 127.0.0.1
user = mysql
password = 123456
database = test111


# 示例代码
import configparser

config = configparser.ConfigParser()
# 读取配置文件
config.read('../etc/config.ini')

# 获取所有项
res = config.sections()
print(f"{res}, {type(res)}")
# ['database_product', 'database_develop'], <class 'list'>

# 获取参数值
res = config['database_develop']['host']
print(f"{res}, {type(res)}")
# 127.0.0.1, <class 'str'>

# 获取参数值
res = config['database_develop'].get('password')
print(f"{res}, {type(res)}")
# 123456, <class 'str'>

# 获取参数值
res = config.get('database_product', 'port')
print(f"{res}, {type(res)}")
# 3306, <class 'str'>

# 获取参数值并设置为int类型
res = config.getint('database_product', 'port')
print(f"{res}, {type(res)}")
# 3306, <class 'int'>

# 获取布尔类型的参数值
res = config.getboolean('database_product', 'is_mysql')
print(f"{res}, {type(res)}")
# True, <class 'bool'>

# 判断是否存在指定配置项
res = config.has_section('test')
print(f"{res}, {type(res)}")
# False, <class 'bool'>

# 判断配置项是否存在指定参数
res = config.has_option('database_product', 'port')
print(f"{res}, {type(res)}")
# True, <class 'bool'>

# 参数不存在时,返回fallback值
res = config.get('database_product', 'xxx', fallback='nononono')
print(f"{res}, {type(res)}")
# nononono, <class 'str'>

# 参数存在时,获取参数值
res = config.get('database_product', 'port', fallback='yesyesyes')
print(f"{res}, {type(res)}")
# 3306, <class 'str'>

种草

我的博文内容主要针对“计算机网络”、“安全”、“运维”和“云计算”方向,感兴趣朋友的请关注我,我将不定期发布新的博文并不断改进已发布博文。

后期依据大家对博文的评论,点赞及关注情况,针对大家感兴趣的内容我也会录制视频并整理出成套的学习资料免费分享给大家,期待能和大家一起交流学习。

  • 10
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

hougang

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值