簡體   English   中英

如何讀取configparser中某個部分下的所有單詞?

[英]How to read all words under a section in configparser?

我正在讀取一個文件,該文件的部分結構如下:

[name]
John
Mary
Ben
John

請注意,沒有鑰匙。

如何使用 ConfigParser 讀取這些值?

謝謝

您應該創建ConfigParser並將關鍵字參數allow_no_value設置為True 您還需要刪除文件中的重復項(John 出現了兩次)。

config = configparser.ConfigParser(allow_no_value=True)
config.read("config_file_name")

for name in config["name"]:
    print(name)

Output:

John
Mary
Ben

如果您需要name部分中的重復項,則必須繼承ConfigParser class 並覆蓋該限制。


邊注:

考慮使用 JSON 或 YAML 而不是 ini 文件來完成這樣的任務,它會讓你的生活輕松

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM