fhand=open('d:/mbox.txt')counts=dict()forlineinfhand:words=line.split()iflen(words)==0:continueifwords[0]!='From':continueifwords[1]notincounts:counts[words[1]]=1else:cou...
fhand = open('d:/mbox.txt')
counts = dict()
for line in fhand:
words = line.split()
if len(words) == 0 :continue
if words[0] != 'From' :continue
if words[1] not in counts:
counts[words[1]] = 1
else:
counts[words[1]] += 1
print counts
fhand = open('d:/mbox.txt')
counts = dict()
for line in fhand:
words = line.split()
if line.startswith('From'):
print words[1]
if words[1] not in counts:
counts[words[1]] = 1
else:
counts[words[1]] += 1
print counts
第二个要在第四行加 line.rstrip()才能运行
文件如下的d:/mbox.txt
From stephen.marquard@uct.ac.za Sat Jan 5 09:14:16 2008
Return-Path:
Received: from murder (mail.umich.edu [141.211.14.90])
为什么第二个提示out of range
展开