[英]How to read all words under a section in configparser?
I'm reading from a file which has a section structured like this:我正在读取一个文件,该文件的部分结构如下:
[name]
John
Mary
Ben
John
Note that there are no keys.请注意,没有钥匙。
How to read those values with ConfigParser?如何使用 ConfigParser 读取这些值?
Thanks谢谢
You should create your ConfigParser
with the keyword argument allow_no_value
set to True
.您应该创建
ConfigParser
并将关键字参数allow_no_value
设置为True
。 Also you need to remove the duplication in your file (John appears twice).您还需要删除文件中的重复项(John 出现了两次)。
config = configparser.ConfigParser(allow_no_value=True)
config.read("config_file_name")
for name in config["name"]:
print(name)
Output: Output:
John
Mary
Ben
If you need the duplications in the name
section, you will have to inherit the ConfigParser
class and override that limitation.如果您需要
name
部分中的重复项,则必须继承ConfigParser
class 并覆盖该限制。
Consider using a JSON or YAML instead of an ini file for such task, it will make your life much easier考虑使用 JSON 或 YAML 而不是 ini 文件来完成这样的任务,它会让你的生活更轻松
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.