为什么我需要在下面的代码中使用 [6] 来切出时间?

编写一个程序来通读 mbox-short.txt 并计算出每条消息按一天中的小时分布。


您可以通过查找时间然后使用冒号再次拆分字符串来从“From”行中提取小时。


一旦你累积了每小时的计数,打印出计数,按小时排序,如下所示。


name = input('Enter file name: ')

if len(name)<1:

    name = 'mbox-short.txt'

hand = open(name)

counts = dict()



for line in hand:

    if not line.startswith('From '):

        continue

    words = line.split(' ')

    words = words[6]

    #print(words.split(':'))

    hour = words.split(':')[0]

    counts[hour] = counts.get(hour, 0) + 1

for k,v in sorted(counts.items()):

     print(k,v)

我必须使用 [6] 来削减电子邮件中的时间。但不应该是5吗?


我需要从中提取小时的行如下所示:来自 stephen.marquard@uct.ac.za Sat Jan 5 09:14:16 200


梦里花落0921
浏览 171回答 1
1回答

胡说叔叔

是的,你是对的,这个例子中的索引应该是 5。顺便说一下,collections模块中有一个内置对象。你可以像这样重写你的代码:from collections import Countercounter = Counter()name = input('Enter file name: ')if len(name) < 1:&nbsp; &nbsp; name = 'mbox-short.txt'with open(name) as fp:&nbsp; &nbsp; for line in fp:&nbsp; &nbsp; &nbsp; &nbsp; if line.startswith('From'):&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; words = line.split(' ')&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; time = words[5]&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; hour = time.split(':')[0]&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; counter[hour] += 1for hour, freq in sorted(counter.items(), key=lambda x: int(x[0])):&nbsp; &nbsp; &nbsp;print(hour, freq)您还可以通过以下方式访问最常见的项目:counter.most_common(10) # it'll show you the first 10 most common items
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python