我正在從INI文件讀取配置某些屏幕按鈕的GUI。我被卡住試圖分開從INI文件返回的一些數據。基本上我有一個按鈕「type」,它是4個選項中的1個,並且基於類型,GUI將爲按鈕分配一個函數。 (我正在使用INI文件,以方便將來更改按鈕功能)Python INI文件如何將1變量的內容分隔成3個單獨的變量
我想要做的是將按鈕分類成組,然後確定哪些按鈕在該組中,自己的變量。
下面是按鈕類型的INI文件:
[Button1]
type = Run Mission
[Button2]
type = Set Register
[Button3]
type = Set Register
[Button4]
type = Set Register
[Button5]
type = Indicator
[Button6]
type = Set Register
[Button7]
type = Data
[Button8]
type = Data
[Button9]
type = Data
這裏是我試圖用拉這個數據的代碼。我正在使用configparser來讀取INI文件。問題是,當我print k
來測試數據進來,它打印type_7, type_9, type_8
,如果可能的話;我需要這些每個在他們自己的變量或以某種方式分開。我沒有包含所有的GUI代碼來縮短髮布時間,但如果需要更多的代碼,請告訴我。我更新的python,並看到了一堆類似的帖子,但似乎無法找到具體做這件事的方式。
type_dict = {}
type_dict['type_1'] = config.get("Button1", "type")
type_dict['type_2'] = config.get("Button2", "type")
type_dict['type_3'] = config.get("Button3", "type")
type_dict['type_4'] = config.get("Button4", "type")
type_dict['type_5'] = config.get("Button5", "type")
type_dict['type_6'] = config.get("Button6", "type")
type_dict['type_7'] = config.get("Button7", "type")
type_dict['type_8'] = config.get("Button8", "type")
type_dict['type_9'] = config.get("Button9", "type")
print type_dict
"""for k, v in type_dict.items():
if v == "Run Mission":
print k
for k, v in type_dict.items():
if v == "Set Register":
print k
for k, v in type_dict.items():
if v == "Indicator":
print k"""
for k, v in type_dict.items():
if v == "Data":
print k
任何幫助,非常感謝。先謝謝你!
投票的解釋? –
好吧,例如,我已經返回多個鍵從字典中返回的最後一個循環,因爲多個鍵符合v ==「Data」的條件。我試圖在自己的變量中返回它們。這樣做的目的是爲了讓我的代碼不需要知道每種類型會有多少,因爲每種類型的數量都可以是動態的。 –