我有一个文件,其中每行都包含以下格式的文件,我想编写一个函数,该函数获取关键字“ Main value”并进行搜索并提供输出。
文件格式:
Main value:value_name,value (1):value_name1,value (2):value_name2 and so on...
需要的输出:
Feature name: Main value
Technical name: value_name
Possible values: value_name1
value_name2
value_name3 and so on...
Possible variables: value (1)
value (2)
value (3) and so on...
到目前为止的代码:
def get_feature_infos(file_path, feature_name):
technical_name = []
possible_variables = []
possible_names = []
with open(file_path, "r") as commands_library:
for line in commands_library:
if feature_name in line:
technical_name.append((line.split(",")[0]).split(":")[1])
possible_variables.append(line.split(",")[1].split(":")[0])
possible_names.append(line.split(",")[1].split(":")[1])
print (" Technical name: ", technical_name[0])
print (" Possible values: ", possible_variables[0])
print (" Possible variables: ", possible_names[0])
但这只打印第一个可能的值,但我要所有可能的值
试试这个。它为我工作:
def get_feature_infos(file_path, feature_name):
technical_name = []
possible_variables = []
possible_names = []
data = []
with open(file_path, "r") as commands_library:
for line in commands_library:
if feature_name in line:
technical_name.append((line.split(",")[0]).split(":")[1])
data = (line.split(","))
data.pop(0)
for shortData in data:
possible_variables.append(shortData.split(":")[0])
possible_names.append(shortData.split(":")[1])
print (" Technical name: ", technical_name[0])
print (" Possible variables: ", possible_variables)
print (" Possible names: ", possible_names)
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句