我是 Python 和 stackoverflow 的新手 :)。我有一个需要打印的 txt 文件。这是名为 DOB 的 txt 文件,包含以下信息:
Orville Wright 21 July 1988
Rogelio Holloway 13 September 1988
Marjorie Figueroa 9 October 1988
Debra Garner 7 February 1988
我需要像这样打印和显示列表:
1. Orville Wright
2. Regelio Holloway
3. ......
项目清单
1. 21 July 1988
2. 13 September 1988
3. .......
我下面的代码两次打开 txt 文件,我认为这不是有效的。帮助!
dob = open('DOB.txt', 'r')
for i, line in enumerate(dob, 1):
name = line.split()
name_surname = str(i) + "." + " " + name[0] + " " + name[1]
print(name_surname)
dob = open('DOB.txt', 'r')
for i, line in enumerate(dob, 1):
date = line.split()
day_to_year = str(i) + "." + " " + date[2] + " " + date[3] + " " + date[4]
print(day_to_year)
dob.close()