#!/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 triển khai 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
config = configparser.ConfigParser[] config.read['db.ini']0 ký tự. 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 bắt đầu
#!/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 với
config = configparser.ConfigParser[] config.read['db.ini']6
host = config['mysql']['host'] user = config['mysql']['user'] passwd = config['mysql']['passwd'] db = config['mysql']['db']
Chúng tôi truy cập các tùy chọn từ phần mysql
host2 = config['postgresql']['host'] user2 = config['postgresql']['user'] passwd2 = config['postgresql']['passwd'] db2 = config['postgresql']['db']
Chúng tôi truy cập các tùy chọn từ phần postgresql
$ python reading_from_file.py MySQL configuration: Host: localhost User: user7 Password: s$cret Database: ydb PostgreSQL configuration: Host: localhost User: user8 Password: mypwd$7 Database: testdb
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 nào được chỉ định không
#!/usr/bin/python import configparser config = configparser.ConfigParser[] config.read['db.ini'] sections = config.sections[] print[f'Sections: {sections}'] sections.append['sqlite'] for section in sections: if config.has_section[section]: print[f'Config file has section {section}'] else: print[f'Config file does not have section {section}']
Ví dụ hoạt động với các phần
$ python sections.py Sections: ['mysql', 'postgresql'] Config file has section mysql Config file has section postgresql Config file does not have section sqlite
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
#!/usr/bin/python import configparser cfg_data = ''' [mysql] host = localhost user = user7 passwd = s$cret db = ydb ''' config = configparser.ConfigParser[] config.read_string[cfg_data] host = config['mysql']['host'] user = config['mysql']['user'] passwd = config['mysql']['passwd'] db = config['mysql']['db'] print[f'Host: {host}'] print[f'User: {user}'] print[f'Password: {passwd}'] print[f'Database: {db}']
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ừ một từ điển bằng phương pháp
host = config['mysql']['host'] user = config['mysql']['user'] passwd = config['mysql']['passwd'] db = config['mysql']['db']0
#!/usr/bin/python import configparser cfg_data = { 'mysql': {'host': 'localhost', 'user': 'user7', 'passwd': 's$cret', 'db': 'ydb'} } config = configparser.ConfigParser[] config.read_dict[cfg_data] host = config['mysql']['host'] user = config['mysql']['user'] passwd = config['mysql']['passwd'] db = config['mysql']['db'] print[f'Host: {host}'] print[f'User: {user}'] print[f'Password: {passwd}'] print[f'Database: {db}']
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