我是python的新手,無法弄清楚爲什麼這段代碼不會產生錯誤消息,也沒有輸出。它讀取一個網絡日誌文件。Python映射器reducer
def mapper(key, line):
parts = line.split("/")
if len(parts) > 2:
return parts[1], 1
return None, 1
def reducer(key, values):
return key, sum(values)
def main():
data = {key,values}
with open('apache.log', 'r') as logfile:
for idx, line in enumerate(logfile):
line = line.strip()
key, val = mapper(idx, line)
if key in data:
data[key].append(val)
else:
data[key] = [val,]
for key, values in data.items():
print reducer(key, values)
日誌文件:
[31/Dec/1994:23:46:48 -0700] "GET 116.gif HTTP/1.0" 200 12053
remote - - [31/Dec/1994:23:50:42 -0700] "GET 2196.ps HTTP/1.0" 200 73941
remote - - [31/Dec/1994:23:55:08 -0700] "GET 45.html HTTP/1.0" 200 5489
remote - - [31/Dec/1994:23:56:55 -0700] "GET 2195.ps HTTP/1.0" 200 522318
remote - - [31/Dec/1994:23:59:37 -0700] "GET 957.ps HTTP/1.0" 200 122146
remote - - [01/Jan/1995:00:31:54 -0700] "GET index.html HTTP/1.0" 200 2797
remote - - [01/Jan/1995:00:31:58 -0700] "GET 2.gif HTTP/1.0" 200 2555
你想在日誌文件???找什麼,什麼是烏拉圭回合的期望輸出 – Hackaholic
Web服務器命中結果: ( '月',43666) ( '月',72114) ( '月',99761 ) ('Apr',65011) – user4745212