Consider the standard web log file in assets/logdata.txt. This file records the access a user makes when visiting a web page (like this one!). Each line of the log has the following items:
- a host (e.g.,
'146.204.224.152'
) - a user_name (e.g.,
'feest6811'
note: sometimes the user name is missing! In this case, use '-' as the value for the username.) - the time a request was made (e.g.,
'21/Jun/2019:15:45:24 -0700'
) - the post request type (e.g.,
'POST /incentivize HTTP/1.1'
note: not everything is a POST!)
Your task is to convert this into a list of dictionaries, where each dictionary looks like the following:
example_dict = {"host":"146.204.224.152",
"user_name":"feest6811",
"time":"21/Jun/2019:15:45:24 -0700",
"request":"POST /incentivize HTTP/1.1"}
This is sample of the txt data file.
I wrote these lines of codes:
import re
def logs():
with open("assets/logdata.txt", "r") as file:
logdata = file.read()
#print(logdata)
pattern="""
(?P<host>.*)
(-\s)
(?P<user_name>\w*)
(\s)
([POST]*)
(?P<time>\w*)
"""
for item in re.finditer(pattern,logdata,re.VERBOSE):
print(item.groupdict())
return(item)
logs()
It helped my in making "host"
and "user_name"
however I can't continue and making the rest of the requirements. can anyone help please?