我有一个这样的值列表:
names_needed = ['PCC', 'PSP', 'ASP']
此外,我正在读取.txt
文件,以根据上述列表提取一些值:
PCC WITH NOTHING
ABB,CAI null V00011 11/06/18
ANDERS,SAND null V000103 07/10/17
KUCHEN,SARA null V00011 03/21/19
PSP SECONDARY
MUNCH,TORY null V000113 04/08/19
ACOSTA,AD null V00010 06/19/17
PCC WITH SOEMTHING
BEC,RUMA null V00011 04/17/19
BECE,COR null V00010 10/25/16
TORRE,M null V0001143 06/06/19
ASP HAS IT
XON,ANDREA null V00011 03/27/19
PSP Wanted
NICK,SON null V00011 05/20/19
JUARE,SABIO null V00011 04/02/19
从这个文本文件中,我想读每一行,然后检查是否存在键值之一,并将所有数据放在该关键字之后的列表中。
类似这样的东西:
PCC:
[ABB,CAI null V00011 11/06/18
ANDERS,SAND null V000103 07/10/17
KUCHEN,SARA null V00011 03/21/19
BEC,RUMA null V00011 04/17/19
BECE,COR null V00010 10/25/16
TORRE,M null V0001143 06/06/19]
PSP:
[MUNCH,TORY null V000113 04/08/19
ACOSTA,AD null V00010 06/19/17
NICK,SON null V00011 05/20/19
JUARE,SABIO null V00011 04/02/19]
ASP:
[XON,ANDREA null V00011 03/27/19]
结果可以是列表的列表,也可以是值的字典。我尝试了以下方法:
names_needed = ['PCC', 'PSP', 'ASP']
## Key word list
key_word_list = []
## Empty list to save all lines
all_lines = []
## Open text file
read_text_file = r'text_file.TXT'
### Open the file
with open(read_text_file) as f:
# For each line
for line in f:
# Strip off white space characters
stripped_line = line.strip()
# Iterate the list of key values **** HERE is WHERE I AM ITERATING KEY WORD
for i in names_needed:
if i in stripped_line:
key_word_list.append(i)
all_lines.append(stripped_line)
else:
break
这没有给我想要的结果。我似乎无法理解当前关键字和下一个关键字之间的界限。
答案 0 :(得分:1)
解决此问题的另一种方法是识别哪些行包含键,并将以下所有行附加到字典中的相应键上。您的文件非常适合该解决方案,因为例如,关键行中的任何地方都没有斜杠:
def is_key(line):
return '/' not in line
输出将采用字典的形式,将每个键映射到属于它的行列表:
keywords = {key: [] for key in names_needed}
现在您可以按以下方式使用这些定义:
key = None
with open(read_text_file) as f:
for line in f:
line = line.strip()
if is_key(line):
key = line.split()[0]
elif key in keywords:
keywords[key].append(line)
您现在可以通过按键访问keywords
:
for key, lines in keywords.items():
print(f'{key}:')
print(f' {"\n ".join(lines)}')
print()