I have a list of values like this,
names_needed = ['PCC', 'PSP', 'ASP']
Also, I have .txt file I am reading into extract some values based on the above list.
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
From this text file, I want to read in each line and then check if one of the key values is present and put all data into a list after that key word. Something like this,
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]
It can be a list of list or it can be a dictionary of values. I tried the following,
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:
# For each line split by new line and get the 0th index
each_line = line.split('\n')[0]
# Strip off white space characters
stripped_line = each_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
This is not giving me the results I want. I can't seem to get to the lines between current key word and next key word.
Any suggestion would be great.
Aucun commentaire:
Enregistrer un commentaire