Common Configuration File Formats and Their Python Parsing Methods
This article introduces the most popular configuration file formats—INI, JSON, TOML, and YAML—explains their structures, compares their advantages and drawbacks, and provides Python code examples for reading and using each format safely and efficiently.
In software development, fixed parameters or constants are often stored in separate configuration files to keep core code clean and avoid duplication across modules.
Python projects typically use settings.py or config.py , but when sharing configurations across non‑Python platforms, a language‑agnostic format is preferable.
Common formats include INI, JSON, TOML, and YAML, all of which can be parsed with Python's standard library or third‑party packages.
INI
INI files consist of sections, keys, and values. Example:
<code>[localdb]
host = 127.0.0.1
user = root
password = 123456
port = 3306
database = mysql
</code>Python's built‑in configparser can read them:
<code>from configparser import ConfigParser
cfg = ConfigParser()
cfg.read("/Users/Bobot/db.ini")
print(cfg.items("localdb"))
</code>Values are returned as strings, so they can be converted to a dictionary and unpacked for database connections:
<code>import pymysql
cfg = ConfigParser()
cfg.read("/Users/Bobot/db.ini")
db_cfg = dict(cfg.items("localdb"))
con = pymysql.connect(**db_cfg)
</code>JSON
JSON is a widely used data‑exchange format that can also serve as a configuration file. Example content saved as db.json :
<code>{
"localdb": {
"host": "127.0.0.1",
"user": "root",
"password": "123456",
"port": 3306,
"database": "mysql"
}
}
</code>Reading it in Python is straightforward:
<code>import json, pprint
with open('/Users/Bobot/db.json') as j:
cfg = json.load(j)['localdb']
pprint(cfg)
</code>JSON parsing is simple but lacks comments and can become unwieldy for deeply nested configurations.
TOML
TOML, proposed by Tom Preston‑Werner, offers a more expressive syntax while remaining easy to read. Example config.toml :
<code>[mysql]
host = "127.0.0.1"
user = "root"
port = 3306
database = "test"
[mysql.parameters]
pool_size = 5
charset = "utf8"
[mysql.fields]
pandas_cols = [ "id", "name", "age", "date"]
</code>Parse it with the toml package:
<code>import toml, os, pprint
cfg = toml.load(os.path.expanduser("~/Desktop/config.toml"))
pprint(cfg)
</code>YAML
YAML is a powerful, human‑readable format widely used for configuration (e.g., Docker Compose). Example config.yaml :
<code>mysql:
host: "127.0.0.1"
port: 3306
user: "root"
password: "123456"
database: "test"
parameter:
pool_size: 5
charset: "utf8"
fields:
pandas_cols:
- id
- name
- age
- date
</code>Use PyYAML to load safely:
<code>import yaml, os, pprint
with open(os.path.expanduser("~/config.yaml"), "r") as f:
cfg = yaml.safe_load(f)
pprint(cfg)
</code>Note that the unsafe load() function can execute arbitrary code; always prefer safe_load() .
Conclusion
The complexity of these formats increases roughly as INI < JSON ≈ TOML < YAML. Choose the one that best fits your project's needs and team workflow.
Python Programming Learning Circle
A global community of Chinese Python developers offering technical articles, columns, original video tutorials, and problem sets. Topics include web full‑stack development, web scraping, data analysis, natural language processing, image processing, machine learning, automated testing, DevOps automation, and big data.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.