In Python development, configuration files play an important role. They allow developers to adjust the behavior of their applications without modifying their code. Whether it is a simple key-value pair or a complex nested structure, configuration files can be flexibly dealt with. This article will introduce in detail five common Python configuration file formats: INI, YAML, JSON, TOML and XML, including their sample files, features, usage scenarios, and parsing code.
1. INI configuration file
1. Sample file ()
[database] host = localhost port = 3306 username = admin password = 123456 [smtp] server = port = 587 username = user@ password = password
2. Features and usage scenarios
The INI file has a simple structure and consists of sections, keys and values. Commonly used for parameter configuration of Windows systems, suitable for storing simple key-value pair configurations.
3. Parsing the code
import configparser # Instantiate the ConfigParser objectconf = () ('', encoding='utf-8') # Get the configuration under the specified sectiondatabase_host = conf['database']['host'] smtp_server = conf['smtp']['server'] print(f"Database host: {database_host}") print(f"SMTP server: {smtp_server}")
4. Code explanation
- Instantiate the object using().
- Use the() method to read the configuration file.
- Access the configuration under the specified section through dictionary.
2. YAML configuration file
1. YAML configuration rules
- Case sensitive: The keys in YAML are case sensitive.
- Indent: YAML uses indentation to represent the hierarchy of data, usually indented using two spaces or a tab character (Tab), but cannot be mixed.
- Comments: Comments in YAML start with the # symbol until the end of the line.
- Data Type: YAML supports a variety of data types, including scalars (strings, integers, floating point numbers, booleans), sequences (lists), maps (dictionaries), and composite structures.
- Quotation marks: Strings can be made using single quotes ('), double quotes ("), or without quotes. When quotes are not used, special characters (such as:, {,}, etc.) need to be escaped; when double quotes are used, special characters and variable interpolation can be included (using ${}); when single quotes are used, special characters in the string will not be escaped.
- Boolean: Booleans in YAML can be represented by true/false (lowercase) or True/False (uppercase), but lowercase is recommended for consistency.
- Null value: Null value in YAML can be represented by null or ~.
- Anchors and alias: YAML supports anchors and aliases, allowing configuration fragments to be reused in files.
2. Supported data structures
Scalar: Single value, such as strings, integers, floating point numbers, and boolean values.
Sequence: A set of ordered values, also known as a list. Use the - symbol to represent the list item.
Mapping: A set of key-value pairs, also known as dictionary or hash. Use the: symbol to separate keys and values, and there can be spaces between keys and values.
Compound structure: Maps and sequences can be used in nesting to form complex data structures.
3. Sample file ()
# This is a complex YAML configuration file example# Define a list containing multiple dictionariesservers: - name: web_server ip: 192.168.1.10 roles: - web - cache settings: max_connections: 1000 timeout: 30s - name: db_server ip: 192.168.1.20 roles: [db, backup] settings: max_connections: 500 backup_frequency: daily # Define a dictionary containing nested dictionaries and listsnetwork: dns_servers: - 8.8.8.8 - 8.8.4.4 default_gateway: 192.168.1.1 subnets: office: cidr: 192.168.1.0/24 dhcp_enabled: true lab: cidr: 10.0.0.0/8 dhcp_enabled: false vlan: 10
4. Features and usage scenarios
YAML files are known for their key-value pairs and nested structures, which are easy to read by humans. Commonly used for complex configuration storage, such as Docker Compose files. Suitable for scenarios where hierarchical configuration is required.
5. Parsing the code
import yaml # Read and parse YAML fileswith open('', 'r') as file: config = yaml.safe_load(file) # Get configuration (example)server_list = config['servers'] network_config = config['network'] # parse output (example)for server in server_list: print(f"Server name: {server['name']}") print(f"IP address: {server['ip']}") print(f"Roles: {', '.join(server['roles'])}") print(f"Settings: max_connections: {server['settings']['max_connections']}") print(f"timeout: {server['settings']['timeout']}") print() print(f"DNS Servers: {', '.join(network_config['dns_servers'])}") print(f"Default Gateway: {network_config['default_gateway']}") print(f"Subnet office: CIDR: {network_config['subnets']['office']['cidr']}") print(f"DHCP Enabled: {network_config['subnets']['office']['dhcp_enabled']}") print(f"Subnet lab: CIDR: {network_config['subnets']['lab']['cidr']}") print(f"DHCP Enabled: {network_config['subnets']['lab']['dhcp_enabled']}") print(f"VLAN: {network_config['vlan']}")
6. Analysis instructions
Use the yaml.safe_load() method to read and parse the YAML file, and load the configuration data into the Python dictionary.
Iterate through the list and nested dictionary through dictionary access to print out configuration information.
List items are traversed using for loop, and dictionary items are traversed by key-value pairs using .items() method.
Pay attention to handling nested structures and conditional judgments (such as checking whether a dictionary contains a certain key).
3. JSON configuration file
1. Sample file ()
{ "database": { "host": "localhost", "port": 3306, "username": "admin", "password": "123456" }, "smtp": { "server": "", "port": 587, "username": "user@", "password": "password" } }
2. Features and usage scenarios
JSON file structure is clear and easy to parse by machine. Commonly used in configuration storage in web development. Suitable for scenarios where cross-language sharing configuration is required.
3. Parsing the code
import json # Read and parse JSON fileswith open('', 'r', encoding='utf-8') as file: config = (file) # Get configurationdatabase_host = config['database']['host'] smtp_server = config['smtp']['server'] print(f"Database host: {database_host}") print(f"SMTP server: {smtp_server}")
4. Code explanation
- Use the() method to read and parse the JSON file.
- Access configuration via dictionary.
4. TOML configuration file
1. Sample file ()
[database] host = "localhost" port = 3306 username = "admin" password = "123456" [smtp] server = "" port = 587 username = "user@" password = "password"
2. Features and usage scenarios
The TOML file structure is simple and easy to read by humans. Commonly used for metadata, dependencies, and tool configurations for Python projects.
3. Parsing the code
The Python standard library does not directly support parsing TOML files, but you can use the third-party library tomllib (Python 3.11 and above) or toml (third-party library).
Take the toml library as an example:
import toml # Read and parse TOML fileswith open('', 'r') as file: config = (file) # Get configurationdatabase_host= config['database']['host'] smtp_server = config['smtp']['server'] print(f"Database host: {database_host}") print(f"SMTP server: {smtp_server}")
4. Code explanation
Use the() method to read and parse the TOML file.
Access configuration via dictionary.
5. XML configuration file
1. Sample file ()
<?xml version="1.0" encoding="UTF-8"?> <configuration> <database> <host>localhost</host> <port>3306</port> <username>admin</username> <password>123456</password> </database> <smtp> <server></server> <port>587</port> <username>user@</username> <password>password</password> </smtp> </configuration>
2. Features and usage scenarios
XML file structure is strict and self-descriptive, and is suitable for storing complex and hierarchical configuration information. Commonly used in configuration of enterprise-level applications and web services.
3. Parsing the code
import as ET #Parse XML filestree = ('') root = () #Get configurationdatabase_host = ('database/host').text smtp_server = ('smtp/server').text print(f"Database host: {database_host}") print(f"SMTP server: {smtp_server}")
4. Code explanation
Use modules to parse XML files.
Position a specific XML element through the find() method and obtain its text content.
Summarize
Different configuration file formats have their own advantages and disadvantages. Which format to choose mainly depends on the specific application scenario and requirements. The INI file has a simple structure, which is suitable for storing simple key-value pair configurations; YAML files are easy to read by humans and are suitable for storing complex hierarchical configurations; JSON file has a clear structure, which is easy to analyze by machine, which is suitable for cross-language sharing configurations; TOML file has a simple structure, which is easy to read by humans and is often used in configurations of Python projects; XML files are strict, self-descriptive, and are suitable for storing complex configuration information. In Python, the corresponding libraries can be used to parse these configuration files, so that configuration information can be easily read and used.
The above is the detailed content of the comprehensive analysis and use of configuration files in Python. For more information about Python configuration files, please pay attention to my other related articles!