如何让Counter计算正确的字符串

时间:2016-08-07 02:36:32

标签: python json counter

我正在尝试让计数器计算下面代码中出现最多的日期。

.link {
    position: relative;
    left: 40%;
}

JSON数据存储为类似

的列表
from collections import Counter

with open('dates.json', 'rb') as f:
    data = f.readlines()

c = Counter(data)
print (c.most_common()[:10])

我希望输出与此类似(从另一个程序中获取)

["Sun Aug 07 01:50:13 +0000 2016", "Sun Aug 07 01:50:13 +0000 2016", "Sun Aug 07 01:50:13 +0000 2016", "Sun Aug 07 01:50:13 +0000 2016", "Sun Aug 07 01:50:13 +0000 2016", "Sun Aug 07 01:50:13 +0000 2016", "Sun Aug 07 01:50:13 +0000 2016", "Sun Aug 07 01:50:13 +0000 2016", "Sun Aug 07 01:50:13 +0000 2016"]

但这是我的输出

[('Sun Aug 07 02:29:45 +0000 2016', 4), ('Sun Aug 07 02:31:05 +0000 2016', 4), ('Sun Aug 07 02:31:04 +0000 2016', 3), ('Sun Aug 07 02:31:08 +0000 2016', 3), ('Sun Aug 07 02:31:22 +0000 2016', 3)]

我真的不明白它在那里算什么

1 个答案:

答案 0 :(得分:1)

而不是readlines(),您应该使用json.load()将JSON数据加载到Python列表中:

import json

with open('dates.json', 'r') as f:
    data = json.load(f)