assignment 8.5

编写一个程序,读取邮箱数据,当遇到一个以From开头的文本行,使用split函数将该行子分解成单词。我们需要抽取From开头的行中第二个单词,即发信人
From [email protected] Sat Jan 5 09:14:16 2008
解析以From开头的行,打印出每行中第二个单词,还可以统计发信人数,在结尾输出总数。

代码如下:

1
2
3
4
5
6
7
8
9
10
11
fname = raw_input("Enter file name: ")
if len(fname) < 1 : fname = "mbox-short.txt"
fh = open(fname)
count = 0
for line in fh:
line = line.rstrip()
if not line.startswith('From ') : continue
words = line.split()
count = count + 1
print words[1]
print "There were", count, "lines in the file with From as the first word"