Python INI文件如何将1个变量的内容分离为3个单独的变量
我正在开发一个GUI,它可以从INI文件中读取一些配置屏幕上的按钮。我一直在尝试分离INI文件返回的一些数据。基本上,我有一个按钮“类型”,它是4个选项中的1个,并且基于该类型,GUI将为按钮分配一个功能。(我正在使用INI文件以便于将来更改按钮功能) 我要做的是将按钮按类型分组,然后将该组中的按钮识别为它们自己的变量 以下是按钮类型的INI文件:Python INI文件如何将1个变量的内容分离为3个单独的变量,python,user-interface,dictionary,ini,Python,User Interface,Dictionary,Ini,我正在开发一个GUI,它可以从INI文件中读取一些配置屏幕上的按钮。我一直在尝试分离INI文件返回的一些数据。基本上,我有一个按钮“类型”,它是4个选项中的1个,并且基于该类型,GUI将为按钮分配一个功能。(我正在使用INI文件以便于将来更改按钮功能) 我要做的是将按钮按类型分组,然后将该组中的按钮识别为它们自己的变量 以下是按钮类型的INI文件: [Button1] type = Run Mission [Button2] type = Set Register [Button3] typ
[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文件。问题是,当Iprint 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
非常感谢您的帮助。提前谢谢你 这将为您提供一个以“数据”作为其值的键列表。这能满足你的需要吗
data_buttons = [k for k, v in type_dict.items() if v == "Data"]
对否决票的解释?例如,我的最后一个for循环从字典中返回多个键,因为多个键满足v==“Data”的条件。我试图在各自的变量中返回它们。这样做的目的是使我的代码不依赖于知道每种类型的数量,因为每种类型的数量可以是动态的。是的,这使它们进入了一个列表,然后我可以像我所想的那样解析为单个变量。谢谢你的建议!