我有一个IIS日志文件,其中包含以下格式的行:
61.245.163.59 - [16 / May / 2013:23:55:09 +0530]“GET /ehrm/Recruitment/Images/divider.gif HTTP / 1.1”404 1245 “http://www.example.com/ehrm/Recruitment/MyApplication.aspx?PRF_ID=000005&digest=6LL4BTSuW9YnE5R4T8k27Q”“Mozilla / 5.0(Windows NT 6.1; rv:20.0)Gecko / 20100101 Firefox / 20.0“GET /ehrm/Recruitment/Images/divider.gif - HTTP / 1.1 www.example.com
我想从中获取一些列并构建一个数据帧。在下面的方法中,它只是构建一个包含一列的数据帧。我想让每个拆分列成为数据帧的一列?另一件事是日志文件行的长度不是唯一的,那么如何通过这样分割来提高获取值的准确性呢?
log_list = []
for line in f:
ip = (line.split(' ')[0])
time = (line.split(' ')[2])
method = (line.split(' ')[4])
status = (line.split(' ')[7])
bytes = (line.split(' ')[8])
referrer = (line.split(' ')[9])
agent = (line.split(' ')[10])
data = ip + ' ' + time + ' ' + method + ' ' + status + ' ' + bytes + ' ' + referrer + ' ' + agent
log_list.append(data)
df = pandas.DataFrame(log_list)
答案 0 :(得分:3)
以下代码应该完成您要执行的操作:
from pandas import read_csv
log_file = 'filename.log'
df = read_csv(log_file, sep=r'\s+', usecols=[0, 2, 4, 7, 8, 9, 10])