Created
December 31, 2013 01:43
-
-
Save shenfeng/8191098 to your computer and use it in GitHub Desktop.
parse nginx access log in python
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def seg_access_log(line): | |
delimiters = {'[': ']', '"': '"'} | |
idx, start, count, delimiter, results = 0, 0, len(line), ' ', [] | |
while 1: | |
idx = line.find(delimiter, start) | |
delimiter = ' ' # reset | |
if idx < 0: | |
break | |
if start < idx: | |
results.append(line[start:idx]) | |
start = idx + 1 | |
# if idx != count - 1 and line[idx + 1] in delimiters: | |
if line[idx + 1] in delimiters: | |
delimiter = delimiters[line[idx + 1]] | |
start += 1 | |
if start < count: | |
results.append(line[start:].rstrip()) | |
return results |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment