一、背景
学习flask时,需要补充配置文件相关知识点
任意文件夹下新建配置文件和读取脚本文件:
二、操作
cofig.ini文件内容:
# 配置文件内容
[section1]
default1 = this is a default value
[databases]
host = 10.10.12.199
port = 3306
user = user123
pwd = 123456789
db = testing
[boolean]
value1 = yes
value2 = no
value3 = 0
value4 = 1
value5 = true
value6 = false
value7 = on
value8 = off
[databases1]
str_conf = %(db)s://%(host)s:%(port)s:%(user)s@%(pwd)s
host = 10.10.12.199
port = 3306
user = user123
pwd = 123456789
db = testing
[data]
default = 10
data_float = 10.5
[DEFAULT]
str_conf = %(db)s://%(host)s:%(port)s:%(user)s@%(pwd)s
host = 10.10.12.199
port = 3306
user = user123
pwd = 123456789
db = testing
[mysqld]
user = mysql
pid-file = /var/run/mysqld/mysqld.pid
读取文件的python脚本:
testconfig.py
"""
********************************************
@Time : 2022/7/7 9:25
@Auth : Domini
@File : testcofnig.py
@IDE : PyCharm
@Motto: Talk is cheap. Show me the code.
********************************************
"""
import os
from configparser import ConfigParser
filepath = os.path.join(os.getcwd(), 'config.ini')
conf = ConfigParser()
conf.read(filepath, encoding='utf-8')
# 1、获取section名和options名
data_sections = conf.sections()
print(data_sections)
data_options = conf.options(data_sections[0])
print(data_options)
# 2、获取section下的option数据
# 返回的数据类型是列表中包含了元祖,这种数据类型可直接强制转成字典
data_items = conf.items('databases')
print(data_items)
print(dict(data_items))
# 3、获取布尔值的数据
value1 = conf.getboolean('boolean', 'value1')
value2 = conf.getboolean('boolean', 'value2')
print(value1, value2)
value3 = conf.getboolean('boolean', 'value3')
value4 = conf.getboolean('boolean', 'value4')
print(value3, value4)
value5 = conf.getboolean('boolean', 'value5')
value6 = conf.getboolean('boolean', 'value6')
print(value5, value6)
value7 = conf.getboolean('boolean', 'value7')
value8 = conf.getboolean('boolean', 'value8')
print(value7, value8)
# 4、格式化输出
data = conf.get('databases1', 'str_conf')
print(data)
# 5、获取整型数据
# 配置文件中所有的数据都是字符串类型,所以获取整形数据必须使用getint()方法获取
data_int = conf.getint('data', 'default')
print(type(data_int))
print(data_int)
# 6、获取浮点型数据
data_float = conf.getfloat('data', 'data_float')
print(data_float)
# 7、获取字符串
data_str = conf.get('data', 'data_float')
print(data_str)
# 8、使用read_dict方法在读取实际文件前设置默认值
conf.read_dict(
{"info": {"host": "10.10.12.66", "port": 3307, "user": "account", "pwd": 123456, "db": "django"}}
)
conf.read(filepath, encoding='utf-8')
print(conf.sections())
print(conf.items('info'))
print(conf.get('info', 'user'))
# 9、使用read_string方法在读取实际文件前配置默认值
sample_config = """
[mysqld]
user = mysql
pid-file = /var/run/mysqld/mysqld.pid
"""
conf = ConfigParser(allow_no_value=True)
conf.read_string(sample_config)
conf.read(filepath, encoding="utf-8")
print(conf.sections())
print(conf.items('mysqld'))
# 10, 直接配置文件设置默认值,section必须是大写的DEFAULT
default_data = conf.default_section
print(default_data)
print(conf.items(default_data))
# 11、判断section是否存在,不存在就返回False, 存在就返回True
print(conf.has_section('rr'))
print(conf.has_section('databases'))
# 12、配置文件写入section和options数据
# if conf.has_section('new_add_section'):
# pass
# else:
# conf.add_section('new_add_section')
# conf.set('new_add_section', 'a_float', '44.78')
#
# with open(filepath, 'a') as f:
# conf.write(f)
三、参考:
python:configparser(配置文件读取操作) (qq.com)https://mp.weixin.qq.com/s/i99lXhLhcftJ0syId1qrAg