I need the following regex code to return all items in the source file as dictionaries such that when I print len(logs()), it returns something like 1000 instead of 4.
code:
def logs():
with open("logdata.txt", "r") as file:
global logdata
logdata = file.read()
pattern = """
(?P<host>.*)
(\ \-\ )
(?P<user_name>[\w]+[\d]+)
(\ \[)
(?P<time>.*)
(\] \ \")
(?P<request>.*)
(\")"""
for item in re.finditer(pattern,logdata,re.VERBOSE):
return item.groupdict()
sample source file: 146.204.224.152 - feest6811 [21/Jun/2019:15:45:24 -0700] "POST /incentivize HTTP/1.1" 302 4622
when I call log function:
{'host': '146.204.224.152',
'user_name': 'feest6811',
'time': '21/Jun/2019:15:45:24 -0700',
'request': 'POST /incentivize HTTP/1.1'}