0

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'}
Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563
Mustapha
  • 23
  • 5
  • `return [item.groupdict() for item in re.finditer(pattern, logdata, re.VERBOSE)]`? – Mustafa Aydın Mar 29 '21 at 20:47
  • Or, replace `return` with `yield` and then you will have a generator as a return value type that you will be able to cast to a `list`. See [this Python demo](https://ideone.com/WWdDPm). – Wiktor Stribiżew Mar 29 '21 at 20:50
  • Does this answer your question? [How to use a return statement in a for loop?](https://stackoverflow.com/questions/44564414/how-to-use-a-return-statement-in-a-for-loop) – mkrieger1 Mar 29 '21 at 21:02
  • Thanks a lot. This worked. My issue is now printing the log function's length. I need it to be equal to the number items in the dictionary. – Mustapha Mar 29 '21 at 21:05

0 Answers0