Question: How to fix the incorrect regex between ### BEGIN CODE and ### END CODE to convert the given data into a list of dictionaries, where

How to fix the incorrect regex between ### BEGIN CODE and ### END CODE to convert the given data 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"}

import re def logs(): with open("assets/logdata.txt", "r") as file: logdata = file.read() ### BEGIN CODE pattern = """ ([.]+) # host %h \s\-\s(.+)\s # user %u \[(.+)\] # time %t \s\"(\w\.+)\" # request %r """ result = [item.groupdict() for item in re.findall(pattern,logdata,re.VERBOSE)]

### END CODE

# YOUR CODE HERE raise NotImplementedError()

return result

logs()

assert len(logs()) == 979

How to fix the incorrect regex between ### BEGIN CODE and ###

*****

Consider the variable 'logdata' which is a string containing a standard web log. This variable 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 username (e.g., feest6811 or sometime '-' since it is missing)
  • 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)

*****

Show Hint In [ ]: import re def logs(): with open("assets/logdata.txt", "r") as file: logdata = file.read() ### BEGIN CODE pattern ([ - ]+) Is\-s(-+) \s [(-+)\] \s\"(\w\.+)" # host % # user %u # time %t # request %r result = [item.groupdict() for item in re.findall(pattern, logdata,re. VERBOSE)] ### END CODE # YOUR CODE HERE raise NotImplementedError() return result logs) In [ ]: assert len(logs() == 979

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!