我想上次我问错问题了enter link description here
我有一个.txt文件,如下所示:
.
.
T - Python and Matplotlib Essentials for Scientists and Engineers
.
A - Wood, M.A.
.
.
.
我想提取行中的一部分并提取每个列表的每个元素,以下是我的脚本:
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 = filter(None, o_t.split('T - '))
list_o_T = [o_T]
o_Title = list_o_T[0]
print (o_Title)
o_A = filter(None, o_a.split('A - '))
list_o_A = [o_A]
o_Lname = list_o_A[0]
o_Fname = list_o_A[1]
print (o_Lname)
print (o_Fname)
以及我想要的输出:
Python and Matplotlib Essentials for Scientists and Engineers
Wood
M.A.
我键入如下脚本:
输出:
相关问题 更多 >
编程相关推荐