Skip to main content
 首页 » 编程设计

python之如何在 Python 中使用循环从 txt 文件中提取单词(行中的第二个和第三个)

2025年05月04日22txw1958

我有几个 txt 文件,其中包含作者的名字和姓氏。 这是大约三十个中的两个示例(不包含相同数量的作者)。

作者1.txt

AU  - Jordan, M.  
AU  - Thomson, J.J.   
AU  - Einstein, A.   
AU  - Tesla, N. 

作者3.txt

AU  - Agassi, A. 
AU  - Herbert, P.H. 
AU  - Agut, R.B.  

我想为每个文件提取作者的姓氏和名字。 由于我是Python的初学者,所以我写了一个脚本(或多或少适合)。

with open('authors3.txt', 'rb') as f: 
    textfile_temp = f.read() 
 
#o_author1  
o_author1 = textfile_temp.split('AU  - ')[1] 
L_name1  = o_author1.split(",")[0] 
F_name1  = o_author1.split(",")[1] 
print(L_name1) 
print(F_name1) 
 
#o_author2  
o_author2 = textfile_temp.split('AU  - ')[2] 
L_name2  = o_author2.split(",")[0] 
F_name2  = o_author2.split(",")[1] 
print(L_name2) 
print(F_name2) 
 
#o_author3  
o_author3 = textfile_temp.split('AU  - ')[3] 
L_name3  = o_author3.split(",")[0] 
F_name3  = o_author3.split(",")[1] 
print(L_name3) 
print(F_name3) 

我的结果是:

Agassi 
 A. 
 
Herbert 
 P.H. 
 
Agut 
 R.B. 

我的问题:知道文件 authors#.txt 不包含相同数量的作者,是否可以编写一个带有循环的脚本?

请您参考如下方法:

使用简单的for循环

演示:

authors_firstName = [] 
authors_lastName = [] 
with open(filename, "r") as infile: 
    for i in infile.readlines(): 
        val = i.strip().split("-")[-1].strip().split(",")   #str.strip to remove any leading or trailing space, split by "-" 
        authors_firstName.append(val[0]) 
        authors_lastName.append(val[1]) 
print(authors_firstName) 
print(authors_lastName) 

输出:

['Jordan', 'Thomson', 'Einstein', 'Tesla', 'Agassi', 'Herbert', 'Agut'] 
[' M.', ' J.J.', ' A.', ' N.', ' A.', ' P.H.', ' R.B.']