日志格式
61.159.140.123 - - [23/Aug/2014:00:01:42 +0800] "GET /favicon.ico HTTP/1.1" 404 \ "-" "Mozilla/5.0 (Windows NT 5.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/29.0.1547.66 Safari/537.36 LBBROWSER" "-"
61.159.140.123 - - [23/Aug/2014:00:01:42 +0800] "GET /favicon.ico HTTP/1.1" 404 \ "-" "Mozilla/5.0 (Windows NT 5.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/29.0.1547.66 Safari/537.36 LBBROWSER" "-"
#!/usr/bin/env python
# -*- coding:utf-8 -*-
with open('/Users/shaoying/Desktop/python/04/www_access_20140823.log') as f:
accessDict = {}
for oneAccess in f.readlines():
oneAccessList = oneAccess.split(' ')
accessDictKey = (oneAccessList[8],oneAccessList[6],oneAccessList[0])
print accessDictKey
if accessDictKey in accessDict:
accessDict[accessDictKey] += 1
else:
accessDict[accessDictKey] = 1
for k,v in accessDict.items():
print [k[0],k[1],(k[2],v)]
61.159.140.123 - - [23/Aug/2014:00:01:42 +0800] "GET /favicon.ico HTTP/1.1" 404 \ "-" "Mozilla/5.0 (Windows NT 5.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/29.0.1547.66 Safari/537.36 LBBROWSER" "-"
61.159.140.123 - - [23/Aug/2014:00:01:42 +0800] "GET /favicon.ico HTTP/1.1" 404 \ "-" "Mozilla/5.0 (Windows NT 5.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/29.0.1547.66 Safari/537.36 LBBROWSER" "-"
#!/usr/bin/env python
# -*- coding:utf-8 -*-
with open('/Users/shaoying/Desktop/python/04/www_access_20140823.log') as f:
accessDict = {}
for oneAccess in f.readlines():
oneAccessList = oneAccess.split(' ')
accessDictKey = (oneAccessList[8],oneAccessList[6],oneAccessList[0])
print accessDictKey
if accessDictKey in accessDict:
accessDict[accessDictKey] += 1
else:
accessDict[accessDictKey] = 1
for k,v in accessDict.items():
print [k[0],k[1],(k[2],v)]