Question: Question: Consider the variable 'logdata' which is a string containing a standard web log. This variable records the access a user makes when visiting a

Question:

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)

Your task is 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"}

Failed info (need detailed explain, thank you!)

The following cell failed:

1 assert len(logs()) == 979 * HIDDEN TESTS The error was:

---------------------------------------------------------------------------

AssertionError Traceback (most recent call last)

in

* HIDDEN TESTS

8

* HIDDEN TESTS

AssertionError: Sorry, this item should be in the log results, check your formating

My answer

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 pattern = """ (?P[\d]*.[\d]*.[\d]*.[\d]*) (\ -\ ) (?P[\w-]*) (\ \[) (?P

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!