简体   繁体   中英

Convert access.log file into json file using python

I'm trying to convert access.log file of nginx into json format but i'm facing following error

Index error: list index out of range

import json

i = 1
result = {}
with open('access.log') as f:
lines = f.readlines()
for line in lines:
    r = str.split('\\s+')
    result[i-1] = {'timestamp': r[0], 'monitorip': r[1], 'monitorhost': r[2], 'monitorstatus': 
    r[3], 'monitorid': r[4], 'resolveip': r[5]}
    i += 1 
    print(result) 
with open('data.json', 'w') as fp:
json.dump(result, fp)

Below is the log format I'm trying to convert

以下是我要转换的日志格式

Error i face is:

Traceback (most recent call last):
File "/home/test.py", line 10, in <module>

result[i-1] = {'timestamp': r[0], 'monitorip': r[1], 'monitorhost': 
r[2], 'monitorstatus': r[3], 'monitorid': r[4], 'resolveip': r[5]}

IndexError: list index out of range

Use line.split() instead of str.split('\\s+') . split() takes a regular string as the delimiter, not a regular expression. And it defaults to using any whitespace as the delimiter.

Check that r has enough fields before trying to use it. This will skip any incomplete lines.

import json

i = 0
result = {}

with open('access.log') as f:
    lines = f.readlines()

for line in lines:
    r = line.split()
    if len(r) >= 6:
        result[i] = {'timestamp': r[0], 'monitorip': r[1], 'monitorhost': r[2], 'monitorstatus': 
                     r[3], 'monitorid': r[4], 'resolveip': r[5]}
        i += 1
    print(result) 

with open('data.json', 'w') as fp:
    json.dump(result, fp)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM