参考文章
ini 配置文件
配置文件的格式如下:中括号“[ ]”内包含的为section。section 下面为类似于key-value 的配置内容。每个section的key可以相同。
# 数据库配置
[db]
host = 192.168.1.133
port = 3306
name = shop
password = 123123
# redis 配置
[redis]
host = 192.168.1.136
port = 6300
password = yes
生成配置文件
# 初始化配置危机
def init_config():
config = configparser.ConfigParser()
config["db"] = {"host": "192.168.1.133",
"port": "3306",
"name": "shop",
"password": "123123"
} # 类似于操作字典的形式
config["redis"] = {"host": "192.168.1.136",
"port": "3309",
"password": "redis"
}
with open("example.ini", "w", encoding="UTF-8") as configfile:
config.write(configfile) # 将对象写入文件
运行结果:生成配置文件 example.ini,内容如下。
[db]
host = 192.168.1.133
port = 3306
name = shop
password = 123123
[redis]
host = 192.168.1.136
port = 3309
password = redis
读取配置
# 读取配置文件
def read_config():
config = configparser.ConfigParser()
config.read("example.ini", encoding="UTF-8")
# 获取指定key的value
# 方式一
db_host = config["db"]["host"]
redis_host = config["redis"]["host"]
print("db_host = %s " % db_host)
print("redis_host = %s " % redis_host)
# 方式二
db_port = config.get("db", "port")
redis_port = config.get("redis", "port")
print("db_port = %s " % db_port)
print("redis_port = %s " % redis_port)
# 方式三 . getint,getboolean,getfloat
db_port = config.getint("db", "port")
redis_port = config.getint("redis", "port")
print("db_port = %s " % db_port)
print("redis_port = %s " % redis_port)
print("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~")
# 判断 section 是否存在
flag = config.has_section("redis")
print(flag)
flag = config.has_section("mq")
print(flag)
# 判断 ption 是否存在
flag = config.has_option("redis", "host")
print(flag)
print("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~")
# 获取 sections
sections = config.sections()
print(sections)
# 获取 section 的 key 集合
options = config.options("db")
print(options)
# 获取 section 的 key-value 集合
items = config.items("db")
print(items)
运行结果:
db_host = 192.168.1.133
redis_host = 192.168.1.136
db_port = 3306
redis_port = 3309
db_port = 3306
redis_port = 3309
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
True
False
True
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
['db', 'redis']
['host', 'port', 'name', 'password']
[('host', '192.168.1.133'), ('port', '3306'), ('name', 'shop'), ('password', '123123')]
修改配置
def update_config():
config = configparser.ConfigParser()
config.read("example.ini", encoding="UTF-8")
# 添加配置
config.add_section("file")
config.set("file", "max_size", "10000")
config.set("file", "extend", ".img,.xml")
# 修改配置
config.set("redis", "port", "6379")
# 删除配置
config.remove_option("redis", "password")
with open('example.ini', 'w') as config_file:
config.write(config_file)
运行结果,example.ini 文件如下:
[db]
host = 192.168.1.133
port = 3306
name = shop
password = 123123
[redis]
host = 192.168.1.136
port = 6379
[file]
max_size = 10000
extend = .img,.xml
RawConfigParser , ConfigParser 区别
RawConfigParser是最基础的INI文件读取类,ConfigParser、SafeConfigParser支持对 %(value)s 变量的解析
ini 添加内容
[message]
name=小明
age=27
detail= name is %(name)s age is %(age)s
def raw_config_parser():
config = configparser.ConfigParser()
config.read("example.ini", encoding="UTF-8")
print("~~~~~~~~~~ConfigParser~~~~~~~~~~~~")
name = config["message"]["name"]
age = config["message"]["age"]
detail = config["message"]["detail"]
print("name = %s , age = %s , detail = %s"% (name, age, detail))
config = configparser.RawConfigParser()
config.read("example.ini", encoding="UTF-8")
print("~~~~~~~~~~ConfigParser~~~~~~~~~~~~")
name = config["message"]["name"]
age = config["message"]["age"]
detail = config["message"]["detail"]
print("name = %s , age = %s , detail = %s"% (name, age, detail))
运行结果:
~~~~~~~~~~ConfigParser~~~~~~~~~~~~
name = 小明 , age = 27 , detail = name is 小明 age is 27
~~~~~~~~~~ConfigParser~~~~~~~~~~~~
name = 小明 , age = 27 , detail = name is %(name)s age is %(age)s