我的程序没有正确地按顺序从文件中读取行

My program is not correctly reading lines from a file sequentially

这是来自 Python For Everyone 第 10 章作业 10.2 的作业,其中问题说明

Write a program to read through the mbox-short.txt and figure out the distribution by hour of the day for each of the messages. You can pull the hour out from the 'From ' line by finding the time and then splitting the string a second time using a colon. "From person@example.com Sat Jan 5 09:14:16 2008" Once you have accumulated the counts for each hour, print out the counts, sorted by hour as shown below.

期望的输出是

04 3
06 1
07 1
09 2
10 3
11 6
14 1
15 2
16 4
17 2
18 1
19 1

我的代码在这里

`name = raw_input("Enter file:")
if len(name) < 1 : name = "mbox-short.txt"
handle = open(name)
counts = dict()

for line in handle:
    line = line.rstrip()
    if line.startswith("From "):
        parts = line.split()
#        print parts
        time = parts[5]
        pieces = time.split(':')
        hour = pieces[0]
        counts[hour] = counts.get(hour,0)+1
print counts `

可以在此处找到文本文件 http://www.pythonlearn.com/code/mbox-short.txt 调试时,我意识到我的编译器多次遍历每一行,每个小时的 return 值太高了。我确信语法 line.startswith("From ") 仅适用于阅读预期的行,因为我在之前的作业中使用过。

如何获得正确的小时频率?

我试过你的代码工作正常。

对于输出字典未排序。 您可以使用 sort(counts) which returns 键的排序列表。有了这些,你可以按排序方式打印你的字典

name = raw_input("Enter file:")
if len(name) < 1 : name = "mbox-short.txt"
handle = open(name)
counts = dict()

for line in handle:
    line = line.rstrip()
    if line.startswith("From "):
        parts = line.split()
        time = parts[5]
        pieces = time.split(':')
        hour = pieces[0]
        counts[hour] = counts.get(hour,0)+1

for key in sorted(counts):
    print key + " " + str(counts[key])

输出是

04 3
06 1
07 1
09 2
10 3
11 6
14 1
15 2
16 4
17 2
18 1
19 1