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'], 

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

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

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

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

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

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

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

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

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

种草

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

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

你可能感兴趣的:(运维,python,开发语言)