项目配置的最常见文件格式(ini,toml,yaml,conf,json,env)

devtools/2025/2/6 22:16:53/

在编程中,有多种文件格式可用于处理项目配置,每种格式都有其特点,下面将详细比较常见的配置文件格式(INI、TOML、YAML、CONF、JSON、ENV)以及用于解析它们的 Python 库。

1. INI 文件格式

  • 特点
    • 简单易读,采用键值对的形式,通过[section]来分组配置项。
    • 适合简单的配置场景,不支持复杂的数据结构。
  • 示例

收起

ini

[database]
host = localhost
port = 5432
username = admin
password = secret[app]
debug = true

  • Python 解析库configparser

收起

python

import configparserconfig = configparser.ConfigParser()
config.read('config.ini')db_host = config.get('database', 'host')
app_debug = config.getboolean('app', 'debug')print(f"Database host: {db_host}")
print(f"App debug mode: {app_debug}")

2. TOML 文件格式

  • 特点
    • 设计目标是成为一个最小化的配置文件格式,易于阅读和编写。
    • 支持丰富的数据类型,如整数、浮点数、字符串、布尔值、数组、表等。
  • 示例

收起

toml

[database]
host = "localhost"
port = 5432
username = "admin"
password = "secret"[app]
debug = true

  • Python 解析库toml

收起

python

import tomlwith open('config.toml', 'r') as f:config = toml.load(f)db_host = config['database']['host']
app_debug = config['app']['debug']print(f"Database host: {db_host}")
print(f"App debug mode: {app_debug}")

3. YAML 文件格式

  • 特点
    • 采用缩进和换行来表示数据结构,可读性强。
    • 支持复杂的数据结构,如列表、字典等,还支持注释。
  • 示例

收起

yaml

database:host: localhostport: 5432username: adminpassword: secret
app:debug: true

  • Python 解析库PyYAML

收起

python

import yamlwith open('config.yaml', 'r') as f:config = yaml.safe_load(f)db_host = config['database']['host']
app_debug = config['app']['debug']print(f"Database host: {db_host}")
print(f"App debug mode: {app_debug}")

4. CONF 文件格式

  • 特点
    • 不是一种标准化的格式,通常根据具体应用自定义格式,常见的形式类似于 INI 文件。
  • 示例

收起

conf

# Database configuration
db_host = localhost
db_port = 5432# Application configuration
app_debug = true

  • Python 解析库:可以使用configparser或自定义解析逻辑

收起

python

import configparserconfig = configparser.ConfigParser()
config.read('config.conf')db_host = config.get('DEFAULT', 'db_host')
app_debug = config.getboolean('DEFAULT', 'app_debug')print(f"Database host: {db_host}")
print(f"App debug mode: {app_debug}")

5. JSON 文件格式

  • 特点
    • 是一种轻量级的数据交换格式,易于机器解析和生成。
    • 支持简单的数据类型(字符串、数字、布尔值、数组、对象)。
  • 示例

收起

json

{"database": {"host": "localhost","port": 5432,"username": "admin","password": "secret"},"app": {"debug": true}
}

  • Python 解析库json

收起

python

import jsonwith open('config.json', 'r') as f:config = json.load(f)db_host = config['database']['host']
app_debug = config['app']['debug']print(f"Database host: {db_host}")
print(f"App debug mode: {app_debug}")

6. ENV 文件格式

  • 特点
    • 主要用于存储环境变量,格式为KEY=VALUE,每行一个配置项。
    • 通常用于在不同环境中配置应用程序,如开发、测试、生产环境。
  • 示例

收起

plaintext

DB_HOST=localhost
DB_PORT=5432
APP_DEBUG=true

  • Python 解析库python-dotenv

收起

python

from dotenv import load_dotenv
import osload_dotenv()db_host = os.getenv('DB_HOST')
app_debug = os.getenv('APP_DEBUG', 'false').lower() == 'true'print(f"Database host: {db_host}")
print(f"App debug mode: {app_debug}")

总结

  • 简单配置场景:INI 和 CONF 适合简单的配置,使用configparser解析。
  • 需要丰富数据类型:TOML 和 JSON 是不错的选择,分别使用tomljson库解析。
  • 复杂数据结构和可读性:YAML 是首选,使用PyYAML库解析。
  • 环境变量配置:ENV 文件适合,使用python-dotenv库加载。

http://www.ppmy.cn/devtools/156635.html

相关文章

AURIX TC275学习笔记3 官方例程 (UART LED WDT)

文章目录 参考资料1. ASCLIN_UART_12. GPIO_LED_Button_13. WDT (Watch Dog Timer) 参考资料 AURIX TC275学习笔记1 资料收集Getting Started with AURIX™ Development Studio 官方帮助文档happy hacking for TC275! 硬件平台使用AURIX™ TC275 Lite 套件,按照参…

STM32 PWM驱动直流电机

接线图: 代码配置: 根据驱动舵机的代码来写,与舵机不同的是,这次的引脚接到了PA2上,所以需要改一下引脚以及改为OC3通道。 另外还需在配置两个GPIO引脚,来控制电机的旋转方向,这里连接到了PA4与…

跨域问题解决实践

在软件开发中,经常会遇到跨域问题,这个问题比较头疼,今天主要介绍下遇到的跨域问题解决思路及如何解决? 1、首先是后端跨域问题 spring boot中的跨域配置如下: Configuration public class WebMvcConfig implements W…

【go语言】结构体

一、type 关键字的用法 在 go 语言中,type 关键字用于定义新的类型,他可以用来定义基础类型、结构体类型、接口类型、函数类型等。通过 type 关键字,我们可以为现有类型创建新的类型别名或者自定义新的类型。 1.1 类型别名 使用 type 可以为…

C32.【C++ Cont】静态实现双向链表及STL库的list

目录 1.知识回顾 2.静态实现演示图 3.静态实现代码 1.初始双向链表 2.头插 3.遍历链表 4.查找某个值 4.任意位置之后插入元素 5.任意位置之前插入元素 6.删除任意位置的元素 4.STL库的list 1.知识回顾 96.【C语言】数据结构之双向链表的初始化,尾插,打印和尾删 97.【C…

人工智能学习(四)之机器学习基本概念

机器学习基本概念详细解析:从生活实例轻松入门 在当今数字化时代,机器学习作为人工智能领域的核心技术之一,正深刻地改变着我们的生活和工作方式。从智能语音助手到图像识别系统,从个性化推荐引擎到自动驾驶汽车,机器…

python算法和数据结构刷题[1]:数组、矩阵、字符串

一画图二伪代码三写代码 LeetCode必刷100题:一份来自面试官的算法地图(题解持续更新中)-CSDN博客 算法通关手册(LeetCode) | 算法通关手册(LeetCode) (itcharge.cn) 面试经典 150 题 - 学习计…

Redis缓存穿透、击穿、雪崩介绍以及解决方案

一、缓存穿透 1.1 什么是缓存穿透? 指的是,外部进来的请求,查询一个不存在的数据。Redis中没有,数据库中也没有,这时候如果外部恶意大量请求,所有请求会直接查询数据库,导致数据库崩溃 1.2 解决…