#!/usr/bin/python import configparser config = configparser.ConfigParser[] config.read['db.ini'] host = config['mysql']['host'] user = config['mysql']['user'] passwd = config['mysql']['passwd'] db = config['mysql']['db'] print['MySQL configuration:'] print[f'Host: {host}'] print[f'User: {user}'] print[f'Password: {passwd}'] print[f'Database: {db}'] host2 = config['postgresql']['host'] user2 = config['postgresql']['user'] passwd2 = config['postgresql']['passwd'] db2 = config['postgresql']['db'] print['PostgreSQL configuration:'] print[f'Host: {host2}'] print[f'User: {user2}'] print[f'Password: {passwd2}'] print[f'Database: {db2}']8 là một lớp Python thực hiện ngôn ngữ cấu hình cơ bản cho các chương trình Python. Nó cung cấp một cấu trúc tương tự như các tệp Microsoft Windows INI.
#!/usr/bin/python import configparser config = configparser.ConfigParser[] config.read['db.ini'] host = config['mysql']['host'] user = config['mysql']['user'] passwd = config['mysql']['passwd'] db = config['mysql']['db'] print['MySQL configuration:'] print[f'Host: {host}'] print[f'User: {user}'] print[f'Password: {passwd}'] print[f'Database: {db}'] host2 = config['postgresql']['host'] user2 = config['postgresql']['user'] passwd2 = config['postgresql']['passwd'] db2 = config['postgresql']['db'] print['PostgreSQL configuration:'] print[f'Host: {host2}'] print[f'User: {user2}'] print[f'Password: {passwd2}'] print[f'Database: {db2}']8 cho phép viết các chương trình Python mà người dùng cuối có thể tùy chỉnh dễ dàng
Tệp cấu hình bao gồm các phần theo sau là các cặp tùy chọn khóa/giá trị. Tên phần được phân cách bằng ký tự
config = configparser.ConfigParser[] config.read['db.ini']0. Các cặp được phân tách bằng
config = configparser.ConfigParser[] config.read['db.ini']1 hoặc
config = configparser.ConfigParser[] config.read['db.ini']2. Nhận xét bắt đầu bằng
config = configparser.ConfigParser[] config.read['db.ini']3 hoặc bằng
config = configparser.ConfigParser[] config.read['db.ini']4
Python ConfigParser đọc tệp
Trong ví dụ đầu tiên, chúng tôi đọc dữ liệu cấu hình từ một tệp
[mysql] host = localhost user = user7 passwd = s$cret db = ydb [postgresql] host = localhost user = user8 passwd = mypwd$7 db = testdb
Chúng tôi có hai phần dữ liệu cấu hình
#!/usr/bin/python import configparser config = configparser.ConfigParser[] config.read['db.ini'] host = config['mysql']['host'] user = config['mysql']['user'] passwd = config['mysql']['passwd'] db = config['mysql']['db'] print['MySQL configuration:'] print[f'Host: {host}'] print[f'User: {user}'] print[f'Password: {passwd}'] print[f'Database: {db}'] host2 = config['postgresql']['host'] user2 = config['postgresql']['user'] passwd2 = config['postgresql']['passwd'] db2 = config['postgresql']['db'] print['PostgreSQL configuration:'] print[f'Host: {host2}'] print[f'User: {user2}'] print[f'Password: {passwd2}'] print[f'Database: {db2}']
Ví dụ đọc dữ liệu cấu hình cho MySQL và PostgreSQL
config = configparser.ConfigParser[] config.read['db.ini']
Chúng tôi khởi tạo
#!/usr/bin/python import configparser config = configparser.ConfigParser[] config.read['db.ini'] host = config['mysql']['host'] user = config['mysql']['user'] passwd = config['mysql']['passwd'] db = config['mysql']['db'] print['MySQL configuration:'] print[f'Host: {host}'] print[f'User: {user}'] print[f'Password: {passwd}'] print[f'Database: {db}'] host2 = config['postgresql']['host'] user2 = config['postgresql']['user'] passwd2 = config['postgresql']['passwd'] db2 = config['postgresql']['db'] print['PostgreSQL configuration:'] print[f'Host: {host2}'] print[f'User: {user2}'] print[f'Password: {passwd2}'] print[f'Database: {db2}']8 và đọc tệp bằng
config = configparser.ConfigParser[] config.read['db.ini']6
#!/usr/bin/python import configparser config = configparser.ConfigParser[] config.read['db.ini'] host = config['mysql']['host'] user = config['mysql']['user'] passwd = config['mysql']['passwd'] db = config['mysql']['db'] print['MySQL configuration:'] print[f'Host: {host}'] print[f'User: {user}'] print[f'Password: {passwd}'] print[f'Database: {db}'] host2 = config['postgresql']['host'] user2 = config['postgresql']['user'] passwd2 = config['postgresql']['passwd'] db2 = config['postgresql']['db'] print['PostgreSQL configuration:'] print[f'Host: {host2}'] print[f'User: {user2}'] print[f'Password: {passwd2}'] print[f'Database: {db2}']2
Chúng tôi truy cập các tùy chọn từ phần mysql
#!/usr/bin/python import configparser config = configparser.ConfigParser[] config.read['db.ini'] host = config['mysql']['host'] user = config['mysql']['user'] passwd = config['mysql']['passwd'] db = config['mysql']['db'] print['MySQL configuration:'] print[f'Host: {host}'] print[f'User: {user}'] print[f'Password: {passwd}'] print[f'Database: {db}'] host2 = config['postgresql']['host'] user2 = config['postgresql']['user'] passwd2 = config['postgresql']['passwd'] db2 = config['postgresql']['db'] print['PostgreSQL configuration:'] print[f'Host: {host2}'] print[f'User: {user2}'] print[f'Password: {passwd2}'] print[f'Database: {db2}']3
Chúng tôi truy cập các tùy chọn từ phần postgresql
#!/usr/bin/python import configparser config = configparser.ConfigParser[] config.read['db.ini'] host = config['mysql']['host'] user = config['mysql']['user'] passwd = config['mysql']['passwd'] db = config['mysql']['db'] print['MySQL configuration:'] print[f'Host: {host}'] print[f'User: {user}'] print[f'Password: {passwd}'] print[f'Database: {db}'] host2 = config['postgresql']['host'] user2 = config['postgresql']['user'] passwd2 = config['postgresql']['passwd'] db2 = config['postgresql']['db'] print['PostgreSQL configuration:'] print[f'Host: {host2}'] print[f'User: {user2}'] print[f'Password: {passwd2}'] print[f'Database: {db2}']4
Dữ liệu cấu hình được tổ chức thành các phần.
config = configparser.ConfigParser[] config.read['db.ini']7 đọc tất cả các phần và
config = configparser.ConfigParser[] config.read['db.ini']8 kiểm tra xem có phần được chỉ định không
#!/usr/bin/python import configparser config = configparser.ConfigParser[] config.read['db.ini'] host = config['mysql']['host'] user = config['mysql']['user'] passwd = config['mysql']['passwd'] db = config['mysql']['db'] print['MySQL configuration:'] print[f'Host: {host}'] print[f'User: {user}'] print[f'Password: {passwd}'] print[f'Database: {db}'] host2 = config['postgresql']['host'] user2 = config['postgresql']['user'] passwd2 = config['postgresql']['passwd'] db2 = config['postgresql']['db'] print['PostgreSQL configuration:'] print[f'Host: {host2}'] print[f'User: {user2}'] print[f'Password: {passwd2}'] print[f'Database: {db2}']7
Ví dụ hoạt động với các phần
#!/usr/bin/python import configparser config = configparser.ConfigParser[] config.read['db.ini'] host = config['mysql']['host'] user = config['mysql']['user'] passwd = config['mysql']['passwd'] db = config['mysql']['db'] print['MySQL configuration:'] print[f'Host: {host}'] print[f'User: {user}'] print[f'Password: {passwd}'] print[f'Database: {db}'] host2 = config['postgresql']['host'] user2 = config['postgresql']['user'] passwd2 = config['postgresql']['passwd'] db2 = config['postgresql']['db'] print['PostgreSQL configuration:'] print[f'Host: {host2}'] print[f'User: {user2}'] print[f'Password: {passwd2}'] print[f'Database: {db2}']8
Python ConfigParser đọc từ chuỗi
Kể từ Python 3. 2, chúng ta có thể đọc dữ liệu cấu hình từ một chuỗi bằng phương thức
config = configparser.ConfigParser[] config.read['db.ini']9
config = configparser.ConfigParser[] config.read['db.ini']0
Ví dụ đọc cấu hình từ một chuỗi
Kể từ Python 3. 2, chúng ta có thể đọc dữ liệu cấu hình từ từ điển bằng phương pháp
#!/usr/bin/python import configparser config = configparser.ConfigParser[] config.read['db.ini'] host = config['mysql']['host'] user = config['mysql']['user'] passwd = config['mysql']['passwd'] db = config['mysql']['db'] print['MySQL configuration:'] print[f'Host: {host}'] print[f'User: {user}'] print[f'Password: {passwd}'] print[f'Database: {db}'] host2 = config['postgresql']['host'] user2 = config['postgresql']['user'] passwd2 = config['postgresql']['passwd'] db2 = config['postgresql']['db'] print['PostgreSQL configuration:'] print[f'Host: {host2}'] print[f'User: {user2}'] print[f'Password: {passwd2}'] print[f'Database: {db2}']20
config = configparser.ConfigParser[] config.read['db.ini']2
Ví dụ đọc cấu hình từ từ điển Python
#!/usr/bin/python import configparser config = configparser.ConfigParser[] config.read['db.ini'] host = config['mysql']['host'] user = config['mysql']['user'] passwd = config['mysql']['passwd'] db = config['mysql']['db'] print['MySQL configuration:'] print[f'Host: {host}'] print[f'User: {user}'] print[f'Password: {passwd}'] print[f'Database: {db}'] host2 = config['postgresql']['host'] user2 = config['postgresql']['user'] passwd2 = config['postgresql']['passwd'] db2 = config['postgresql']['db'] print['PostgreSQL configuration:'] print[f'Host: {host2}'] print[f'User: {user2}'] print[f'Password: {passwd2}'] print[f'Database: {db2}']0
Khóa là tên phần, giá trị là từ điển có khóa và giá trị có trong phần