2016-08-07 73 views
0

我想讓計數器計算哪個日期出現在下面的代碼中最多。如何讓計數器計算正確的字符串

from collections import Counter 

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

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

JSON數據被存儲爲像

["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)] 

但是,這是我的輸出

[(48, 72), (32, 53), (49, 27), (34, 18), (117, 18), (58, 18), (65, 9), (51, 9), (103, 9), (43, 9)] 

我真的不明白其計數有

回答

1

相反的readlines(),你應該使用json.load()的JSON數據加載到一個Python列表:

import json 

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

現在我們有錯誤類型錯誤:JSON對象必須str的,而不是'字節' – Mark

+1

@Mark從文件打開模式中刪除'b'... –