Cách đọc tệp conf trong python?

Khi chủ đề về cấu hình hoặc cấu hình, các tệp xuất hiện, thường có sự nhầm lẫn về chúng là gì, cách đọc chúng và những lợi ích đến từ việc sử dụng chúng. Trong bài viết này, chúng tôi sẽ giới thiệu các tệp cấu hình là gì trước khi giới thiệu mô-đun Python

[postgresql]
host = localhost
user = finxter1
password = myfinxterpw
db = postgres

[user_info]
admin = David Yeoman
login = finxter_freelancer
password = freelancer_freedom
0. Sau đó, chúng tôi sẽ sử dụng một số ví dụ để giúp bạn hiểu, tạo, đọc và cập nhật các tệp cấu hình của riêng bạn

Giới thiệu nhanh về tệp cấu hình

Mặc dù chủ yếu được sử dụng bởi các hệ điều hành để tùy chỉnh môi trường, các tệp cấu hình có thể được sử dụng cho nhiều mục đích sử dụng. Các trường hợp sử dụng phổ biến nhất đối với lập trình viên hoặc nhà phát triển là định cấu hình cài đặt ban đầu cho chương trình mà không cần dữ liệu mã hóa cứng vào chính chương trình. Điều này cho phép bạn thay đổi các cài đặt đó mà không cần phải vào chương trình và tự chỉnh sửa mã nguồn. Tóm lại, những thứ có thể thay đổi theo thời gian như địa chỉ IP, tên máy chủ, tên người dùng, mật khẩu, tên cơ sở dữ liệu, đều có thể được cấu trúc thành một tệp cấu hình và chương trình hướng đến tệp đó để biết thông tin cụ thể cần thiết. Đối với những người muốn cung cấp quyền truy cập công khai vào mã của họ trên một kho lưu trữ như GitHub, các tệp cấu hình có thể được bao gồm trong tệp

[postgresql]
host = localhost
user = finxter1
password = myfinxterpw
db = postgres

[user_info]
admin = David Yeoman
login = finxter_freelancer
password = freelancer_freedom
1 để đảm bảo chúng không thể truy cập công khai, do đó bảo vệ thông tin nhạy cảm

Chúng có cấu trúc như thế nào?

Tôi không biết về bất kỳ tiêu chuẩn hoặc quy ước nào liên quan đến bố cục của tệp cấu hình. Một số định dạng cho phép dễ dàng lưu trữ các cấu trúc dữ liệu khá phức tạp để phục hồi và sử dụng sau này. Yếu tố quyết định về định dạng bạn sử dụng sẽ phụ thuộc vào ngôn ngữ lập trình của bạn dễ dàng xử lý nhất và sở thích cá nhân của riêng bạn. Với sự quan tâm của chúng tôi đối với Python, tôi sẽ tập trung vào một định dạng phổ biến nhưng thực sự, bầu trời là giới hạn đối với những gì bạn có thể sử dụng. Các định dạng phổ biến bao gồm INI, JSON, YAML và XML. Hôm nay chúng ta sẽ tập trung vào định dạng INI

Tạo, đọc và cập nhật cấu hình. tập tin ini

Ngày trước, Microsoft Windows chủ yếu dựa vào các tệp khởi tạo hoặc tệp INI để định cấu hình cài đặt ứng dụng và hệ điều hành. Trong Python, chúng tôi có một mô-đun tên là

[postgresql]
host = localhost
user = finxter1
password = myfinxterpw
db = postgres

[user_info]
admin = David Yeoman
login = finxter_freelancer
password = freelancer_freedom
0 cho phép chúng tôi dễ dàng tạo, đọc và cập nhật các tệp INI. Nếu bạn chưa cài đặt nó, hãy sử dụng lệnh

$ pip install configparser

Cấu trúc của tệp INI

Mỗi tệp INI bạn tạo sẽ bao gồm các phần trong đó dữ liệu được lưu trữ bằng cách sử dụng các cặp khóa-giá trị theo cách tương tự như kiểu dữ liệu từ điển Python. Sau đây là mẫu viết tay của tệp INI mà chúng tôi sẽ tạo, sau đó chúng tôi sẽ đọc từ tệp đó, trước khi cập nhật tệp cuối cùng

Tên tệp dự kiến. tập tin cấu hình. ban đầu

[postgresql]
host = localhost
user = finxter1
password = myfinxterpw
db = postgres

[user_info]
admin = David Yeoman
login = finxter_freelancer
password = freelancer_freedom

Lưu ý rằng tệp INI được đề xuất này bao gồm hai phần,

[postgresql]
host = localhost
user = finxter1
password = myfinxterpw
db = postgres

[user_info]
admin = David Yeoman
login = finxter_freelancer
password = freelancer_freedom
3 và
[postgresql]
host = localhost
user = finxter1
password = myfinxterpw
db = postgres

[user_info]
admin = David Yeoman
login = finxter_freelancer
password = freelancer_freedom
4. Dưới mỗi phần là các khóa dữ liệu [máy chủ, người dùng, mật khẩu, v.v. ] và các giá trị dữ liệu [
[postgresql]
host = localhost
user = finxter1
password = myfinxterpw
db = postgres

[user_info]
admin = David Yeoman
login = finxter_freelancer
password = freelancer_freedom
5, v.v. ]

Vì vậy,

[postgresql]
host = localhost
user = finxter1
password = myfinxterpw
db = postgres

[user_info]
admin = David Yeoman
login = finxter_freelancer
password = freelancer_freedom
6 này có thể hữu ích nếu mã của chúng tôi cần tạo truy vấn SQL cho cơ sở dữ liệu Postgres của chúng tôi hoặc nếu chúng tôi cần đăng nhập vào một trang web cụ thể yêu cầu đặc quyền quản trị viên, thông tin đăng nhập và dữ liệu mật khẩu

Bạn có thể thêm bao nhiêu phần tùy thích cho nhiều trường hợp sử dụng

Tạo một tập tin cấu hình mới

Mở một tệp Python mới, trong đó chúng tôi sẽ viết mã để tạo tệp INI. Đoạn mã sau phần lớn là tự giải thích. Lưu ý rằng trong đoạn mã này, tôi giả định rằng hiện tại không có tệp cùng tên nào tồn tại do đó lệnh

[postgresql]
host = localhost
user = finxter1
password = myfinxterpw
db = postgres

[user_info]
admin = David Yeoman
login = finxter_freelancer
password = freelancer_freedom
7 cho
[postgresql]
host = localhost
user = finxter1
password = myfinxterpw
db = postgres

[user_info]
admin = David Yeoman
login = finxter_freelancer
password = freelancer_freedom
8 ở dòng thứ hai đến dòng cuối cùng của mã. Dưới đây là các bước

  1. Nhập mô-đun
    [postgresql]
    host = localhost
    user = finxter1
    password = myfinxterpw
    db = postgres
    
    [user_info]
    admin = David Yeoman
    login = finxter_freelancer
    password = freelancer_freedom
    9
  2. Tạo đối tượng
    [postgresql]
    host = localhost
    user = finxter1
    password = myfinxterpw
    db = postgres
    
    [user_info]
    admin = David Yeoman
    login = finxter_freelancer
    password = freelancer_freedom
    9 và chuyển nó tới
    import configparser
    
    config = configparser.ConfigParser[]
    
    # Add the structure to the file we will create
    config.add_section['postgresql']
    config.set['postgresql', 'host', 'localhost']
    config.set['postgresql', 'user', 'finxter1']
    config.set['postgresql', 'port', '5543']
    config.set['postgresql', 'password', 'myfinxterpw']
    config.set['postgresql', 'db', 'postgres']
    
    config.add_section['user_info']
    config.set['user_info', 'admin', 'David Yeoman']
    config.set['user_info', 'login', 'finxter_freelancer']
    config.set['user_info', 'password', 'freelancer_freedom']
    
    # Write the new structure to the new file
    with open[r"C:\PythonTutorials\configfile.ini", 'w'] as configfile:
        config.write[configfile]
    
    
    1
  3. Tạo tên phần
  4. Điền vào phần bằng khóa. cặp giá trị [chẳng hạn như
    import configparser
    
    config = configparser.ConfigParser[]
    
    # Add the structure to the file we will create
    config.add_section['postgresql']
    config.set['postgresql', 'host', 'localhost']
    config.set['postgresql', 'user', 'finxter1']
    config.set['postgresql', 'port', '5543']
    config.set['postgresql', 'password', 'myfinxterpw']
    config.set['postgresql', 'db', 'postgres']
    
    config.add_section['user_info']
    config.set['user_info', 'admin', 'David Yeoman']
    config.set['user_info', 'login', 'finxter_freelancer']
    config.set['user_info', 'password', 'freelancer_freedom']
    
    # Write the new structure to the new file
    with open[r"C:\PythonTutorials\configfile.ini", 'w'] as configfile:
        config.write[configfile]
    
    
    2 trong ví dụ của chúng tôi]
  5. Lặp lại các mục 3 và 4 cho bất kỳ phần tiếp theo nào
  6. Cuối cùng, mở một tệp có tên bạn đã chọn tại vị trí thư mục đã chọn bằng cách sử dụng
    [postgresql]
    host = localhost
    user = finxter1
    password = myfinxterpw
    db = postgres
    
    [user_info]
    admin = David Yeoman
    login = finxter_freelancer
    password = freelancer_freedom
    7 để viết
  7. Viết tập tin

Vì vậy, đây là mã làm theo các bước trên

import configparser

config = configparser.ConfigParser[]

# Add the structure to the file we will create
config.add_section['postgresql']
config.set['postgresql', 'host', 'localhost']
config.set['postgresql', 'user', 'finxter1']
config.set['postgresql', 'port', '5543']
config.set['postgresql', 'password', 'myfinxterpw']
config.set['postgresql', 'db', 'postgres']

config.add_section['user_info']
config.set['user_info', 'admin', 'David Yeoman']
config.set['user_info', 'login', 'finxter_freelancer']
config.set['user_info', 'password', 'freelancer_freedom']

# Write the new structure to the new file
with open[r"C:\PythonTutorials\configfile.ini", 'w'] as configfile:
    config.write[configfile]

Thực thi tệp và kiểm tra thư mục bạn đã nhập trong mã để tìm tệp mới. Đây là ảnh chụp màn hình của tệp tôi đã tạo bằng mã ở trên

Xin chúc mừng. Bạn đã viết tệp INI đầu tiên của mình. Bây giờ, làm thế nào để chúng ta sử dụng nó?

Đọc một tập tin cấu hình

Vì lợi ích của bài tập này, giả sử bạn muốn tạo một truy vấn SQL từ một số mã nguồn mới mà bạn đang viết. Chúng tôi sẽ đọc tệp cấu hình và sử dụng dữ liệu như thế nào? . Sẽ dễ dàng nhận thấy sự khác biệt. Đây là các bước không có tệp cấu hình

  1. Nhập SQLAlchemy [công cụ ưa thích của tôi cho các truy vấn Postgresql]
  2. Tiếp theo, bạn cần đặt tất cả các tham số sẽ tạo thành URI để cho phép kết nối với cơ sở dữ liệu
  3. Tạo URI và công cụ kết nối của bạn và chuyển nó tới một biến
  4. Kết nối với cơ sở dữ liệu của bạn

Đây là mã bạn sẽ sử dụng mà không cần sử dụng tệp cấu hình

import sqlalchemy

# set your parameters for the database connection URI
user = "finxter1"
password = "myfinxterpw"
host = "localhost"
port = 5543
dbase = "postgres"

# Create the URI and the engine with the URI in the following format
# postgresql://username:password@hostname:port/database
# therefore we pass the URI the parameters established above

conn = sqlalchemy.create_engine['postgresql://user:password@host:port/dbase', encoding='utf8']

# Connect to the database
connection = conn.raw_connection[]

Vì vậy, khi chúng tôi thực thi mã này, chúng tôi hiện đã kết nối với cơ sở dữ liệu của mình, nhưng có một vấn đề rõ ràng. Chúng tôi đã mã hóa cứng tên người dùng và mật khẩu của mình vào mã nguồn để tất cả mọi người có thể xem. Không thông minh

Bây giờ, hãy sử dụng cùng mã này với một số bổ sung để cho phép chúng tôi sử dụng

[postgresql]
host = localhost
user = finxter1
password = myfinxterpw
db = postgres

[user_info]
admin = David Yeoman
login = finxter_freelancer
password = freelancer_freedom
6 mà chúng tôi đã tạo ở trên. Chúng tôi có thêm hai bước,
import configparser

config = configparser.ConfigParser[]

# Add the structure to the file we will create
config.add_section['postgresql']
config.set['postgresql', 'host', 'localhost']
config.set['postgresql', 'user', 'finxter1']
config.set['postgresql', 'port', '5543']
config.set['postgresql', 'password', 'myfinxterpw']
config.set['postgresql', 'db', 'postgres']

config.add_section['user_info']
config.set['user_info', 'admin', 'David Yeoman']
config.set['user_info', 'login', 'finxter_freelancer']
config.set['user_info', 'password', 'freelancer_freedom']

# Write the new structure to the new file
with open[r"C:\PythonTutorials\configfile.ini", 'w'] as configfile:
    config.write[configfile]

5 và sau đó đọc
[postgresql]
host = localhost
user = finxter1
password = myfinxterpw
db = postgres

[user_info]
admin = David Yeoman
login = finxter_freelancer
password = freelancer_freedom
6. Dưới đây là các bước

  1. Nhập SQLAlchemy
  2. Nhập trình cấu hình
  3. Đọc tập tin cấu hình. ban đầu
  4. Đọc các thông số từ configfile. ini sẽ tạo thành một URI để kết nối với cơ sở dữ liệu
  5. Tạo URI và công cụ kết nối của bạn và chuyển nó tới một biến
  6. Kết nối với cơ sở dữ liệu của bạn

Đây là mã bạn sẽ sử dụng khi kết nối bằng tệp INI của mình

import sqlalchemy
import configparser

#Read config.ini file
config_obj = configparser.ConfigParser[]
config_obj.read["C:\PythonTutorials\configfile.ini"]
dbparam = config_obj["postgresql"]
useinfo = config_obj["user_info"]

# set your parameters for the database connection URI using the keys from the configfile.ini
user = dbparam["user"]
password = dbparam["password"]
host = dbparam["host"]
port = int[dbparam["port"]]
dbase = dbparam["db"]

# Create the URI and the engine with the URI in the following format
# postgresql://username:password@hostname:port/database
# so we pass the URI the parameters established above

conn = sqlalchemy.create_engine['postgresql://user:password@host:port/dbase', encoding='utf8']

# Connect to the database
connection = conn.raw_connection[]

Như bạn có thể thấy, chúng tôi cần sử dụng

[postgresql]
host = localhost
user = finxter1
password = myfinxterpw
db = postgres

[user_info]
admin = David Yeoman
login = finxter_freelancer
password = freelancer_freedom
9 để tạo một đối tượng, sau đó đọc tệp cấu hình. ini mà chúng tôi đã tạo trước đó. Đã đọc tệp cấu hình. ini, chúng tôi đã chuyển từng phần của tệp thành các biến riêng biệt, trong trường hợp này, tham số
[postgresql]
host = localhost
user = finxter1
password = myfinxterpw
db = postgres

[user_info]
admin = David Yeoman
login = finxter_freelancer
password = freelancer_freedom
3 thành
import configparser

config = configparser.ConfigParser[]

# Add the structure to the file we will create
config.add_section['postgresql']
config.set['postgresql', 'host', 'localhost']
config.set['postgresql', 'user', 'finxter1']
config.set['postgresql', 'port', '5543']
config.set['postgresql', 'password', 'myfinxterpw']
config.set['postgresql', 'db', 'postgres']

config.add_section['user_info']
config.set['user_info', 'admin', 'David Yeoman']
config.set['user_info', 'login', 'finxter_freelancer']
config.set['user_info', 'password', 'freelancer_freedom']

# Write the new structure to the new file
with open[r"C:\PythonTutorials\configfile.ini", 'w'] as configfile:
    config.write[configfile]

9 và tham số
[postgresql]
host = localhost
user = finxter1
password = myfinxterpw
db = postgres

[user_info]
admin = David Yeoman
login = finxter_freelancer
password = freelancer_freedom
4 thành useinfo

#Read config.ini file
config_obj = configparser.ConfigParser[]
config_obj.read["C:\PythonTutorials\configfile.ini"]
dbparam = config_obj["postgresql"]
useinfo = config_obj["user_info"]

Vì vậy, thay vì mã hóa cứng các giá trị thành các tham số cho kết nối cơ sở dữ liệu như chúng tôi đã làm trước đây, giờ đây chúng tôi có thể gọi từng giá trị chúng tôi cần từ tệp cấu hình. ini bằng cách sử dụng các phím và gán chúng cho các biến

# set your parameters for the database connection URI using the keys from the configfile.ini
user = dbparam["user"]
password = dbparam["password"]
host = dbparam["host"]
port = int[dbparam["port"]]
dbase = dbparam["db"]

Để chứng minh rằng dữ liệu đã được chuyển một cách thích hợp, hãy sử dụng lệnh in để kiểm tra

import sqlalchemy
import configparser

#Read config.ini file
config_obj = configparser.ConfigParser[]
config_obj.read["C:\PythonTutorials\configfile.ini"]
dbparam = config_obj["postgresql"]
useinfo = config_obj["user_info"]

# set your parameters for the database connection URI using the keys from the configfile.ini
user = dbparam["user"]
password = dbparam["password"]
host = dbparam["host"]
port = int[dbparam["port"]]
dbase = dbparam["db"]

print['User variable = ', user, '\n']
print['Password variable = ', password, '\n']
print['Host variable = ', host, '\n']
print['Port variable = ', port, '\n']
print['Database variable = ', dbase, '\n']

# Result

User variable =  finxter1

Password variable =  myfinxterpw

Host variable =  localhost

Port variable =  5543

Database variable =  postgres


Process finished with exit code 0

Vì vậy, phải thừa nhận thêm một chút mã trong ví dụ thứ hai, nhưng hãy nhìn vào những lợi ích bạn nhận được

  • Bạn không cần mã hóa cứng thông tin nhạy cảm vào một tệp mà người khác có thể khám phá
  • Bất kể bạn sử dụng các tham số trong mã của mình bao nhiêu lần, nếu bạn cần thay đổi giá trị của chúng, bạn chỉ cần chỉnh sửa tệp cấu hình
  • Nếu bạn muốn đăng mã của mình lên kho lưu trữ như GitHub, bạn có thể đặt tệp cấu hình trong tệp
    [postgresql]
    host = localhost
    user = finxter1
    password = myfinxterpw
    db = postgres
    
    [user_info]
    admin = David Yeoman
    login = finxter_freelancer
    password = freelancer_freedom
    1 để đảm bảo người khác không thể truy cập thông tin bí mật của bạn, sau đó bạn thêm tệp readme để hướng dẫn người khác cách tạo tệp cấu hình mới bằng mã của họ.

Cập nhật tập tin cấu hình của bạn

Đây chính là sự đơn giản. Đây là hai lựa chọn

Đầu tiên là mở

[postgresql]
host = localhost
user = finxter1
password = myfinxterpw
db = postgres

[user_info]
admin = David Yeoman
login = finxter_freelancer
password = freelancer_freedom
6 trong notepad và thực hiện các thay đổi theo cách thủ công. Hãy thay đổi các chi tiết của
import sqlalchemy

# set your parameters for the database connection URI
user = "finxter1"
password = "myfinxterpw"
host = "localhost"
port = 5543
dbase = "postgres"

# Create the URI and the engine with the URI in the following format
# postgresql://username:password@hostname:port/database
# therefore we pass the URI the parameters established above

conn = sqlalchemy.create_engine['postgresql://user:password@host:port/dbase', encoding='utf8']

# Connect to the database
connection = conn.raw_connection[]
3 từ
import sqlalchemy

# set your parameters for the database connection URI
user = "finxter1"
password = "myfinxterpw"
host = "localhost"
port = 5543
dbase = "postgres"

# Create the URI and the engine with the URI in the following format
# postgresql://username:password@hostname:port/database
# therefore we pass the URI the parameters established above

conn = sqlalchemy.create_engine['postgresql://user:password@host:port/dbase', encoding='utf8']

# Connect to the database
connection = conn.raw_connection[]
4 thành
import sqlalchemy

# set your parameters for the database connection URI
user = "finxter1"
password = "myfinxterpw"
host = "localhost"
port = 5543
dbase = "postgres"

# Create the URI and the engine with the URI in the following format
# postgresql://username:password@hostname:port/database
# therefore we pass the URI the parameters established above

conn = sqlalchemy.create_engine['postgresql://user:password@host:port/dbase', encoding='utf8']

# Connect to the database
connection = conn.raw_connection[]
5

Bây giờ hãy chạy lại mã

import sqlalchemy
import configparser

#Read config.ini file
config_obj = configparser.ConfigParser[]
config_obj.read["C:\PythonTutorials\configfile.ini"]
dbparam = config_obj["postgresql"]
useinfo = config_obj["user_info"]

# set your parameters for the database connection URI using the keys from the configfile.ini
user = dbparam["user"]
password = dbparam["password"]
host = dbparam["host"]
port = int[dbparam["port"]]
dbase = dbparam["db"]

print['User variable = ', user, '\n']
print['Password variable = ', password, '\n']
print['Host variable = ', host, '\n']
print['Port variable = ', port, '\n']
print['Database variable = ', dbase, '\n']

# Result

User variable =  coding_finxter8765

Password variable =  myfinxterpw

Host variable =  localhost

Port variable =  5543

Database variable =  postgres

Tùy chọn thứ hai là sử dụng

[postgresql]
host = localhost
user = finxter1
password = myfinxterpw
db = postgres

[user_info]
admin = David Yeoman
login = finxter_freelancer
password = freelancer_freedom
9 từ bên trong Python. Hãy thay đổi mật khẩu PostgreSQL từ
import sqlalchemy

# set your parameters for the database connection URI
user = "finxter1"
password = "myfinxterpw"
host = "localhost"
port = 5543
dbase = "postgres"

# Create the URI and the engine with the URI in the following format
# postgresql://username:password@hostname:port/database
# therefore we pass the URI the parameters established above

conn = sqlalchemy.create_engine['postgresql://user:password@host:port/dbase', encoding='utf8']

# Connect to the database
connection = conn.raw_connection[]
7 thành
import sqlalchemy

# set your parameters for the database connection URI
user = "finxter1"
password = "myfinxterpw"
host = "localhost"
port = 5543
dbase = "postgres"

# Create the URI and the engine with the URI in the following format
# postgresql://username:password@hostname:port/database
# therefore we pass the URI the parameters established above

conn = sqlalchemy.create_engine['postgresql://user:password@host:port/dbase', encoding='utf8']

# Connect to the database
connection = conn.raw_connection[]
8. Đây là mã

import configparser

# Read config.ini file
edit = configparser.ConfigParser[]
edit.read["C:/PythonTutorials/configfile.ini"]

#Get the postgresql section
postgresql = edit["postgresql"]

#Update the password
postgresql["password"] = "finxter_coders_rock"

#Write changes back to file
with open['C:/PythonTutorials/configfile.ini', 'w'] as configfile:
    edit.write[configfile]

Đây là kết quả

để tóm tắt

Hôm nay chúng ta đã thảo luận về cấu hình hoặc tệp

import configparser

config = configparser.ConfigParser[]

# Add the structure to the file we will create
config.add_section['postgresql']
config.set['postgresql', 'host', 'localhost']
config.set['postgresql', 'user', 'finxter1']
config.set['postgresql', 'port', '5543']
config.set['postgresql', 'password', 'myfinxterpw']
config.set['postgresql', 'db', 'postgres']

config.add_section['user_info']
config.set['user_info', 'admin', 'David Yeoman']
config.set['user_info', 'login', 'finxter_freelancer']
config.set['user_info', 'password', 'freelancer_freedom']

# Write the new structure to the new file
with open[r"C:\PythonTutorials\configfile.ini", 'w'] as configfile:
    config.write[configfile]

1. Chúng là gì, tại sao chúng được sử dụng và một số định dạng phổ biến được sử dụng để cấu trúc chúng

Chúng tôi đã giới thiệu mô-đun

[postgresql]
host = localhost
user = finxter1
password = myfinxterpw
db = postgres

[user_info]
admin = David Yeoman
login = finxter_freelancer
password = freelancer_freedom
9 được sử dụng trong Python và viết một tập lệnh để tạo một tệp INI mới có tên là
[postgresql]
host = localhost
user = finxter1
password = myfinxterpw
db = postgres

[user_info]
admin = David Yeoman
login = finxter_freelancer
password = freelancer_freedom
6

Sau đó, chúng tôi đã minh họa việc sử dụng [và nguy cơ] của các giá trị được mã hóa cứng khi tạo kết nối SQL, trước khi sửa đổi tập lệnh kết nối SQL để cho phép chúng tôi truy cập các giá trị cần thiết từ tệp cấu hình. ini mà chúng tôi đã tạo

Cuối cùng, chúng tôi đã chứng minh việc sửa đổi tệp INI dễ dàng như thế nào bằng cách sửa đổi thủ công một số dữ liệu bằng trình xử lý văn bản trước khi chúng tôi quay lại mô-đun

[postgresql]
host = localhost
user = finxter1
password = myfinxterpw
db = postgres

[user_info]
admin = David Yeoman
login = finxter_freelancer
password = freelancer_freedom
9 để sửa đổi thêm

Hãy nhớ rằng - Tôi đã sử dụng một ví dụ ở đây về việc tạo kết nối cơ sở dữ liệu, nhưng bạn có thể sử dụng tệp cấu hình cho bất kỳ dữ liệu nào bạn muốn giữ an toàn hoặc cho dữ liệu thường được sử dụng trong một số mã nguồn và đôi khi có thể thay đổi. Mọi thay đổi có thể được thực hiện một lần bằng cách chỉnh sửa tệp cấu hình thay vì sắp xếp qua hàng trăm dòng mã và chỉnh sửa thủ công tất cả các lần xuất hiện

Tôi hy vọng bài viết này đã được sử dụng cho bạn. Cảm ơn vì đã đọc

David Yeoman

David là một lập trình viên Python và là một nhà văn kỹ thuật tạo ra các bài báo chuyên sâu dành cho những độc giả muốn có những lời giải thích đơn giản cho các chủ đề gây khó khăn bởi thuật ngữ chuyên ngành. Cũng là một thợ mộc, thợ kim loại, nhiếp ảnh gia phong cảnh và phi công, anh ấy làm nghề tự do sau 42 năm làm việc trong thế giới doanh nghiệp. Anh ấy có bằng MBA về Công nghệ

Làm cách nào để đọc tệp cấu hình XML trong Python?

Để phân tích cú pháp tệp XML, chúng tôi sẽ sử dụng mô-đun BeautifulSoup cùng với trình phân tích cú pháp html . Trước tiên, chúng ta cần cài đặt gói BeautifulSoup4 mới nhất bằng lệnh sau. Sau đó, chúng tôi phải nhập mô-đun BeautifulSoup từ bs4 [BeautifulSoup4]. Bây giờ, tất cả chúng ta đã sẵn sàng để phân tích tệp của mình.

Làm cách nào để đọc các giá trị từ tệp INI trong Python?

Tệp Python. # Đọc tệp và tạo nếu tệp không tồn tại config = iniFile[ 'FILE. INI' ] # Nhận cấu hình "default_path". default_path # Cấu hình in [chuỗi]/đường dẫn/tên in. default_path # Tạo hoặc cập nhật cấu hình.

Là gì. conf bằng Python?

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.

Chủ Đề