python四種配置文件


參考資料:Python中文社區


1. ini

.ini 文件是Initialization File的縮寫,即初始化文件,是windows的系統配置文件所采用的存儲格式,統管windows的各項配置。

1.1 ini文件的定義

.ini 文件通常由(Section)、(key)和(value)組成。具體形式如下:

; 關於mysql的一個小配置
; db.ini
[mysql]
host = 127.0.0.1
port = 3306
user = root
password = 123456
database = test

1.2 python讀取ini文件

使用python內置的 configparser 標准庫進行解析ini文件。

read() 讀取文件內容
items() 獲取指定節的所有鍵值對

# -*- coding: utf-8 -*-
'''
 * @Author       : bpf
 * @Date         : 2020-10-14 22:11:06
 * @Description  : 讀取ini文件
 * @LastEditTime : 2020-10-14 22:41:53
'''

from configparser import ConfigParser
from pprint import pprint
import mysql.connector as MySQL

ini_file = "E:\\CODE\\Python\\src\\test\\TestConfiguration\\db.ini"
db_name = "mysql"

cfg = ConfigParser()
# 讀取文件內容
cfg.read(ini_file)

# cfg.items()返回list,元素為tuple
db_cfg = dict(cfg.items(db_name))

# 打印參數
pprint(db_cfg)

# 連接數據庫
con = MySQL.connect(**db_cfg)
con.close()

讀取ini文件

2. json

JSON(JavaScript Object Notation,) 是一種輕量級的數據交換格式。

2.1 json文件的定義

語法格式:

菜鳥教程

易百教程

極客學院WiKi

html中文網 - json在線手冊

W3school - cn

簡單小示例:

{
    "mysql": {
        "host": "127.0.0.1",
        "port": 3306,
        "user": "root",
        "password": "123456",
        "database": "test"
    }
}

2.2 python讀取json文件

使用python內置的 json 標准庫進行解析ini文件。

load() 從json文件中讀取json格式數據
loads() 將字符串類型數據轉化為json格式數據
dump() 將json格式數據保存到文件
dumps() 將json格式數據保存為字符串類型

'''
 * @Author       : bpf
 * @Date         : 2020-10-14 22:39:44
 * @Description  : 讀取json文件
 * @LastEditTime : 2020-10-14 23:17:48
'''

import json
from pprint import pprint
import mysql.connector as MySQL

json_file = "E:\\CODE\\Python\\src\\test\\TestConfiguration\\db.json"
db_name = "mysql"

with open(json_file) as f:
    cfg = json.load(f)[db_name]

pprint(cfg)

con = MySQL.connect(**cfg)
con.close()

讀取json文件

3. toml

TOML 是 Github 聯合創始人 Tom Preston-Werner 所提出的一種配置文件格式,是一種旨在成為一個小規模、易於使用的語義化的配置文件格式,它被設計為可以無二義性的轉換為一個哈希表。

3.1 定義toml文件

語法:

TOML的語法廣泛地由key = "value"、[節名]、#注釋構成。
支持以下數據類型:字符串、整形、浮點型、布爾型、日期時間、數組和圖表。

# db.toml
[mysql]
    [mysql.config]
    host     = "127.0.0.1"
    user     = "root"
    port     = 3306
    password = "123456"
    database = "test"

    [mysql.parameters]
    pool_size = 5
    charset   = "utf8"

    [mysql.fields]
    course_cols = ["cno", "cname", "ccredit", "cdept"]

3.2 python讀取toml文件

使用外部庫 toml 解析toml文件。

安裝:pip install toml
語法:CSDN

# -*- coding: utf-8 -*-
'''
 * @Author       : bpf
 * @Date         : 2020-10-14 23:24:05
 * @Description  : 讀取toml文件
 * @LastEditTime : 2020-10-14 23:31:07
'''
import toml
from pprint import pprint
import mysql.connector as MySQL

toml_file = "E:\\CODE\\Python\\src\\test\\TestConfiguration\\db.toml"

cfg = toml.load(toml_file)

pprint(cfg)

conn = MySQL.connect(**cfg["mysql"]['config'])
conn.close()

讀取toml文件

4. yaml

YAML(YAML Ain't a Markup Language", YAML不是一種標記語言) 格式是目前較為流行的一種配置文件,它早在 2001 由一個名為 Clark Evans 的人提出;同時它也是目前被廣泛使用的配置文件類型。

4.1 定義yaml文件

語法:

菜鳥教程 - 入門教程

易百教程

知乎

# db.yaml
mysql:
  config:
    host: "127.0.0.1"
    port: 3306
    user: "root"
    password: ""
    database: "stu_sys"
  
  parameters:
    pool_size: 5
    charset: "utf8"

  fileds:
    course_cols:
      - cno
      - cname
      - ccredit
      - cdept

4.2 python讀取yaml文件

使用外部庫 pyyaml 解析toml文件。

安裝:pip install pyyaml
語法:

極客教程

知乎

# -*- coding: utf-8 -*-
'''
 * @Author       : bpf
 * @Date         : 2020-10-14 23:34:37
 * @Description  : 讀取yaml文件
 * @LastEditTime : 2020-10-14 23:39:58
'''
import yaml
from pprint import pprint
import mysql.connector as MySQL

yaml_file = "E:\\CODE\\Python\\src\\test\\TestConfiguration\\db.yaml"

with open(yaml_file, 'r') as f:
    cfg = yaml.safe_load(f)

pprint(cfg)

conn = MySQL.connect(**cfg['mysql']['config'])
conn.close()

讀取yaml文件


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM