簡體   English   中英

使用 ConfigParser 多次指定相同的選項

[英]specifying the same option multiple times with ConfigParser

我想用 python ConfigParser 模塊讀取配置文件:

[asection]
option_a = first_value
option_a = second_value

我希望能夠獲得為選項“option_a”指定的值列表。 我嘗試了以下明顯的方法:

test = """[asection]
option_a = first_value
option_a = second_value
"""
import ConfigParser, StringIO
f = StringIO.StringIO(test)
parser = ConfigParser.ConfigParser()
parser.readfp(f)
print parser.items()

哪些輸出:

[('option_a', 'second_value')]

雖然我希望:

[('option_a', 'first_value'), ('option_a', 'second_value')]

或者,甚至更好:

[('option_a', ['first_value', 'second_value'])]

有沒有辦法用 ConfigParser 做到這一點? 另一個想法?

似乎可以讀取同一個鍵的多個選項,這可能會有所幫助: 如何配置文件為相同的鍵保留多個值?

暫無
暫無

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

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