gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-04 08:34:13 24 4
gpt4 key购买 nike

我有几个 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.']

关于python - 如何在 Python 中使用循环从 txt 文件中提取单词(行中的第二个和第三个),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50406312/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com