Cfg 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 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

Tệp CFG 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.

Cấu hình [] Python là gì?

Cấu hình Python có thể được sử dụng để xây dựng Python tùy chỉnh hoạt động như Python thông thường . Ví dụ: các biến môi trường và đối số dòng lệnh được sử dụng để định cấu hình Python. Cấu hình biệt lập có thể được sử dụng để nhúng Python vào một ứng dụng. Nó cô lập Python khỏi hệ thống.

Các tệp CFG được sử dụng để làm gì?

Một tập tin với. CFG hoặc. Phần mở rộng tệp CONFIG là tệp cấu hình được các chương trình khác nhau sử dụng để lưu trữ các cài đặt dành riêng cho phần mềm tương ứng của chúng. Một số tệp cấu hình là tệp văn bản thuần túy nhưng những tệp khác có thể được lưu trữ ở định dạng dành riêng cho chương trình

Thiết lập CFG được sử dụng để làm gì trong Python?

Thiết lập. cfg là tệp ini, chứa các tùy chọn mặc định để thiết lập. lệnh py . Bạn có thể chỉ định khá nhiều từ khóa mà chúng tôi đã sử dụng trong quá trình thiết lập. py trong thiết lập mới. cfg và chỉ cần sử dụng thiết lập. py làm giao diện dòng lệnh.

Chủ Đề