1 回答

TA貢獻1831條經驗 獲得超9個贊
從非結構化的 txt 文件中讀取配置并不是最好的主意。Python 實際上能夠解析以某種方式構造的配置文件。我已經重組了您的 txt 文件,以便更容易使用。配置文件擴展名并不重要,在本例中我將其更改為 .ini。
應用程序.ini:
[csvfilepath]
inputCsvFile = BIN+"/testing.csv"
description = "testing"
代碼:
from configparser import ConfigParser? # Available by default, no install needed.
config = ConfigParser()? # Create a ConfigParser instance.
config.read('app.ini')? # You can input the full path to the config file.
file_path = config.get('csvfilepath', 'inputCsvFile')
file_description = config.get('csvfilepath', 'description')
print(f"CSV File Path: {file_path}\nCSV File Description: {file_description}")
輸出:
CSV File Path: BIN+"/testing.csv"
CSV File Description: "testing"
添加回答
舉報