Python INI文件如何将1个变量的内容分成3个独立的变量
Python INI File How to Separate Contents of 1 Variable into 3 Separate Variables
我正在开发一个 GUI,它从 INI 文件中读取数据来配置一些屏幕按钮。我一直在尝试分离从 INI 文件返回的一些数据。本质上,我有一个按钮 "type",它是 4 个选项之一,并且根据类型,GUI 将为该按钮分配一个功能。 (我正在使用 INI 文件,以便将来轻松更改按钮功能)
我想要做的是将按钮分类为类型组,然后将该组中的哪些按钮识别到它们自己的变量中。
这是按钮类型的 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 代码以保持 post 简短,但如果需要更多代码,请告诉我。我是 python 的新手,见过很多类似的 post,但似乎找不到具体的方法。
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"。这能满足您的需求吗?
data_buttons = [k for k, v in type_dict.items() if v == "Data"]
我正在开发一个 GUI,它从 INI 文件中读取数据来配置一些屏幕按钮。我一直在尝试分离从 INI 文件返回的一些数据。本质上,我有一个按钮 "type",它是 4 个选项之一,并且根据类型,GUI 将为该按钮分配一个功能。 (我正在使用 INI 文件,以便将来轻松更改按钮功能)
我想要做的是将按钮分类为类型组,然后将该组中的哪些按钮识别到它们自己的变量中。
这是按钮类型的 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 代码以保持 post 简短,但如果需要更多代码,请告诉我。我是 python 的新手,见过很多类似的 post,但似乎找不到具体的方法。
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"。这能满足您的需求吗?
data_buttons = [k for k, v in type_dict.items() if v == "Data"]