使用 configparser 读取特定文本

问题描述 投票:0回答:1

我有一个 INI 文件,我只想读取站名。 INI 文件如下所示

[StationData]
StationName = "DPT11"

[Debug]
ProberTouchDown = "TRUE"

我刚刚了解到使用 configparser 而不是 pandas 来解析这个文本文件会更容易。我希望只得到“DPT11”或 DPT11。我该怎么做?

我试着复制了一些关于这个问题的例子。虽然无法真正弄清楚。 在 python 中使用 configparser

python configparser
1个回答
0
投票

使用 python 3.10.6 尝试以下操作:

剧本内容:

import configparser
config = configparser.ConfigParser()
config.read("src.ini")

for key, value in config["StationData"].items():
    print(value)

python3 script.py
的输出应该是:

"DPT11"
© www.soinside.com 2019 - 2024. All rights reserved.