我需要阅读一个文本文件,然后分别打印信息。 例如: 我收到了以下格式的姓名:Orville Wright 1988年7月21日 我需要这样得出结果: 名称 1.奥维尔·赖特 日期 1988年7月21日
我曾尝试使用阅读器来分隔,但是对于每个给定的名称和日期,我都必须使用单独的代码行,因为它们的长度不一样。
with open('File name and location', 'r') as reader:
print(reader.readline(14))
``````````````````````````````````````````````````
this is the outcome : Orville Wright
```````````````````````````````````````````````````
I want my results to be:
Name:
1. Orville Wright
2. Rogelio Holloway
etc
Date:
1. 21 July 1988
2. 13 September 1988
etc
````````````````````````````````````````````````````
The contents of the file are as follows:
Orville Wright 21 July 1988
Rogelio Holloway 13 September 1988
Marjorie Figueroa 9 October 1988
Debra Garner 7 February 1988
Tiffany Peters 25 July 1988
Hugh Foster 2 June 1988
Darren Christensen 21 January 1988
Shelia Harrison 28 July 1988
Ignacio James 12 September 1988
Jerry Keller 30 February 1988
Frankie Cobb 1 July 1988
Clayton Thomas 10 December 1988
Laura Reyes 9 November 1988
Danny Jensen 19 September 1988
Sabrina Garcia 20 October 1988
Winifred Wood 27 July 1988
Juan Kennedy 4 March 1988
Nina Beck 7 May 1988
Tanya Marshall 22 May 1988
Kelly Gardner 16 August 1988
Cristina Ortega 13 January 1988
Guy Carr 21 June 1988
Geneva Martinez 5 September 1988
Ricardo Howell 23 December 1988
Bernadette Rios 19 July 1988
答案 0 :(得分:1)
这是使用正则表达式的一种方法。
例如:
import re
names = []
dates = []
with open(filename) as infile:
for line in infile:
line = line.strip()
date = re.search("(\d{1,2} [a-zA-Z]+ \d{4})", line).group(1) #Extract Date.
dates.append(date)
names.append(line.replace(date, "").strip()) #Get Name.
print("Name:")
for name in names:
print(name)
print("---"*10)
print("Date:")
for date in dates:
print(date)
输出:
Name:
Orville Wright
Rogelio Holloway
Marjorie Figueroa
Debra Garner
Tiffany Peters
Hugh Foster
Darren Christensen
Shelia Harrison
Ignacio James
Jerry Keller
Frankie Cobb
Clayton Thomas
Laura Reyes
Danny Jensen
Sabrina Garcia
Winifred Wood
Juan Kennedy
Nina Beck
Tanya Marshall
Kelly Gardner
Cristina Ortega
Guy Carr
Geneva Martinez
Ricardo Howell
Bernadette Rios
------------------------------
Date:
21 July 1988
13 September 1988
9 October 1988
7 February 1988
25 July 1988
2 June 1988
21 January 1988
28 July 1988
12 September 1988
30 February 1988
1 July 1988
10 December 1988
9 November 1988
19 September 1988
20 October 1988
27 July 1988
4 March 1988
7 May 1988
22 May 1988
16 August 1988
13 January 1988
21 June 1988
5 September 1988
23 December 1988
19 July 1988
答案 1 :(得分:0)
将所有名称和日期存储在不同的列表中,然后显示每个名称和日期。
以下代码假定每个名称和日期都由换行符分隔,并且该行中的第一位数字是日期的开始。
import re
names = []
dates = []
with open('File name and location', 'r') as reader:
for line in reader.readlines():
date_position = re.search("\d", line).start()
names.append(line[:date_position - 1])
dates.append(line[date_position:])
现在,您可以根据自己的喜好打印每个名称和日期:
for i, name in enumerate(names):
print(f"{i+1}. {name}")
对于日期:
for i, date in enumerate(dates):
print(f"{i+1}. {name}")
输出(部分文本文件):
1. Orville Wright
2. Rogelio Holloway
3. Marjorie Figueroa
4. Debra Garner
5. Tiffany Peters
6. Hugh Foster
7. Darren Christensen
8. Shelia Harrison
9. Ignacio James
10. Jerry Keller
11. Frankie Cobb
12. Clayton Thomas
13. Laura Reyes
14. Danny Jensen
15. Sabrina Garcia
16. Winifred Wood
17. Juan Kennedy
1. 21 July 1988
2. 13 September 1988
3. 9 October 1988
4. 7 February 1988
5. 25 July 1988
6. 2 June 1988
7. 21 January 1988
8. 28 July 1988
9. 12 September 1988
10. 30 February 1988
11. 1 July 1988
12. 10 December 1988
13. 9 November 1988
14. 19 September 1988
15. 20 October 1988
16. 27 July 1988
17. 4 March 1988