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"
添加回答
举报