使用csv显示因错误而短缺的不同日期的总错误数和警告数
我的csv文件格式:
Date Level Message
2016-04-23 04:44:43.472132 [debug] email error
2016-04-23 04:44:43.472132 [debug] error
2016-04-23 04:44:43.472132 [warning] debug error
2016-04-23 04:44:43.472132 [warning] warning message
预期输出格式:
Date Errors Warnings
2015-11-23 45 320
2015-11-22 20 200
答案 0 :(得分:0)
logs = {}
with open("log.csv") as csv_file:
for line in csv_file:
date, time, type, message = line.split(None, 4)
log_by_date = logs.setdefault(date, {})
log_by_date[type] = log_by_date.get(type, 0) + 1
print(logs)
这应该有用......