我正在尝试从 nginx 日志文件中解析以下格式:
10.0.0.1 [02/Oct/2012:10:21:46 +0000] GET /api/123/test.json?stop=2012-09-29 502 0
我的 python 脚本:
#!/usr/bin/env python
f = file('slow-queries.txt', 'r')
# iterate over the lines in the file
for line in f:
# split the line into a list of column values
columns = line.split(' ')
# clean any whitespace off the items
# columns = [col.strip() for col in columns]
# ensure the column has at least one value before printing
if columns:
print "first =>", columns[0] # print the first column
print "second =>", columns[1]
基本上我想从日志文件中得到的只是发送的查询,所以在上面的示例中,我希望提取 /api/123/test.json?stop=2012-09-29
我的脚本似乎没有这样做,我做错了什么?
最佳答案
那些是制表符,不是空格:
ip, date, method, path, status, _ = line.split('\t')
关于python - 以空格作为分隔符解析 nginx 日志数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15023105/