Tập tin cấu hình 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}')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 Show 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ệpTrong 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ỗiKể 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 Tệp cấu hình trong Python là gì?Tệp cấu hình được sử dụng để lưu trữ các cặp giá trị khóa hoặc một số thông tin có thể định cấu hình có thể được đọc hoặc truy cập trong mã và tại một số thời điểm.
Python có tệp cấu hình không?Python có thể có các tệp cấu hình với tất cả các cài đặt mà ứng dụng cần một cách linh hoạt hoặc định kỳ. Các tệp cấu hình Python có phần mở rộng là. ban đầu. Chúng tôi sẽ sử dụng VS Code (Visual Studio Code) để tạo một phương thức chính sử dụng tệp cấu hình để đọc cấu hình và sau đó in trên bảng điều khiển.
Làm cách nào để ghi tệp cấu hình bằng Python?Tạo tệp cấu hình mới . Nhập mô-đun configparser Tạo đối tượng configparser và chuyển nó tới 'config' Tạo tên phần Điền vào phần bằng khóa. các cặp giá trị (chẳng hạn như 'host' = 'local_host' trong ví dụ của chúng tôi) Lặp lại các mục 3 và 4 cho bất kỳ phần tiếp theo nào Tệp py cấu hình ở đâu?cấu hình. tệp py¶ |