Skip to content

配置文件加载类

更新: 2025/10/8 字数: 0 字 时长: 0 分钟

配置文件加载类用于加载配置文件,支持多种格式的配置文件,如:JSON、YAML、INI、TOML。获取键值支持使用.符号进行层级访问。

加载配置

加载JSON格式配置文件

加载JSON格式配置文件

load_json(config_file: str='config/config.json')

如果需要自定义配置文件路径,可以传入config_file参数。

python
from sch.config import Config

config = Config.load_json()

加载YAML格式配置文件

加载YAML格式配置文件

load_yaml(config_file: str='config/config.yaml')

如果需要自定义配置文件路径,可以传入config_file参数。

python
from sch.config import Config

config = Config.load_yaml()

加载INI格式配置文件

加载INI格式配置文件

load_ini(config_file: str='config/config.ini')

如果需要自定义配置文件路径,可以传入config_file参数。

python
from sch.config import Config

config = Config.load_ini()

加载TOML格式配置文件

加载TOML格式配置文件

load_toml(config_file: str='config/config.toml')

如果需要自定义配置文件路径,可以传入config_file参数。

python
from sch.config import Config

config = Config.load_toml()

加载XML格式配置文件

加载XML格式配置文件

load_xml(config_file: str='config/config.xml')

如果需要自定义配置文件路径,可以传入config_file参数。

python
from sch import Config

config = Config.load_xml()
xml
<?xml version="1.0" encoding="UTF-8" ?>
<config>
    <config key="mysql">
        <config key="host">localhost</config>
        <config key="port">3306</config>
        <config key="user">root</config>
        <config key="pass">123456</config>
        <config key="name">root</config>
    </config>
</config>

获取键值

获取键值

get(key: str)

获取键值,支持使用.符号进行层级访问,可以添加默认值,在未特殊定义时值为None

python
from sch.config import Config

config = Config.load_json()
config.get('mysql.host','localhost')
输出内容

localhost