从一行中拆分字符串并附加到 python 3 中的列表中
spliting string from a line and appending in a list in python 3
我有一个文件,其中每一行都包含以下格式我想编写一个函数来获取关键字 "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])
但这只打印第一个可能的值,但我想要所有可能的值
我相信您正在寻找的是一种打印列表中的值列表的方法,而不管列表的命名方式如何。您可以做的是创建一个文本列表。例如:
print (" Technical name: ", ', '.join(technical_name))
您可能还想在打印前通过创建元组来转置输出:
tups = zip(*(technical_name, possible_variables, possible_names))
for tup in tups:
print('{}\t{}\t{}'.format(tup))
试试这个。它对我有用:
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)
我给大家介绍一下str.split
-maxsplit
的第二个参数。
使用maxsplit
,您可以拆分"Main value",其余的:
feature_name, rest = line.split(':', 1)
然后 value_name 和其余的:
technical_name, rest = rest.split(',', 1)
然后 possible_variables 和 possible_names
possible_variables = [x.split(':')[0] for x in rest.split(',')]
possible_names = [x.split(':')[1] for x in rest.split(',')]
我有一个文件,其中每一行都包含以下格式我想编写一个函数来获取关键字 "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])
但这只打印第一个可能的值,但我想要所有可能的值
我相信您正在寻找的是一种打印列表中的值列表的方法,而不管列表的命名方式如何。您可以做的是创建一个文本列表。例如:
print (" Technical name: ", ', '.join(technical_name))
您可能还想在打印前通过创建元组来转置输出:
tups = zip(*(technical_name, possible_variables, possible_names))
for tup in tups:
print('{}\t{}\t{}'.format(tup))
试试这个。它对我有用:
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)
我给大家介绍一下str.split
-maxsplit
的第二个参数。
使用maxsplit
,您可以拆分"Main value",其余的:
feature_name, rest = line.split(':', 1)
然后 value_name 和其余的:
technical_name, rest = rest.split(',', 1)
然后 possible_variables 和 possible_names
possible_variables = [x.split(':')[0] for x in rest.split(',')]
possible_names = [x.split(':')[1] for x in rest.split(',')]