I have a file .txt that contains a specific line, like this
file.txt
.
.
T - Python and Matplotlib Essentials for Scientists and Engineers
.
A - Wood, M.A.
.
.
.
I would like to extract lines that contain a string, I tried with a simple script:
with open('file.txt','r') as f:for line in f:if "T - " in line:o_t = line.rstrip('\n')elif "A - " in line:o_a = line.rstrip('\n')o_T = o_t.split('T - ')
print (o_T)o_A = o_a.split('A - ')
#o_Fname =
#o_Lname =
print (o_A)
my output:
['', 'Python and Matplotlib Essentials for Scientists and Engineers']
['', 'Wood, M.A.']
and my desired output:
Python and Matplotlib Essentials for Scientists and Engineers
Wood, M.A.
moreover, for the second ("Wood, M.A.") can I also extract the last name and first name. So the final results will be:
Python and Matplotlib Essentials for Scientists and EngineersWoodM.A.