我想更改我的代码的输出。我有这样的代码:更改我的代码输出 - Python
from collections import defaultdict
third = defaultdict(lambda: (defaultdict(lambda : defaultdict(int))))
count = 0
fh = open("C:/Users/mycomp/desktop/data.txt", "r").readlines()
for line in fh:
line_split = line.split();
date = line_split[0];
time = line_split[1];
ip = line_split[2];
third [date][time][ip]+= 1
for date, d in third.iteritems():
for time , count in d.iteritems():
print "%s %s %s %s" % (date, time, count,ip)
日志文件是这样的:
2016-11-04 00:00:12 10.11.13.13
2016-11-05 00:00:15 10.14.12.11
2016-11-06 00:00:19 10.10.15.13
我的输出类似下面。
2016-10-04 07:46 defaultdict(<type 'int'>, {'10.11.13.15': 574}) 10.11.13.15
2016-10-04 15:58 defaultdict(<type 'int'>, {'10.21.24.13': 364}) 10.21.24.13
2016-10-04 15:59 defaultdict(<type 'int'>, {'10.21.22.13': 359}) 10.21.22.13
2016-10-04 07:42 defaultdict(<type 'int'>, {'10.21.27.10': 287}) 10.21.27.10
2016-10-04 07:43 defaultdict(<type 'int'>, {'10.11.37.13': 337}) 10.11.37.13
,但我想的输出是这样的:
2016-10-04 07:46 {'10.11.13.15': 574}) 10.11.13.15
2016-10-04 15:58 {'10.21.24.13': 364}) 10.21.24.13
2016-10-04 15:59 {'10.21.22.13': 359}) 10.21.22.13
2016-10-04 07:42 {'10.21.27.10': 287}) 10.21.27.10
2016-10-04 07:43 {'10.11.37.13': 337}) 10.11.37.13
打印“%s%s%s%s”%(date,dict(time),count,ip)试试这一行。 –