菜鸟请教,问题到底出在哪呢?
给定题目:
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"}
编写代码:
import re
def logs():
log_items = []
pattern = r'(\S+) (\S+)?\[(.*?)\] "(.*?)"'
with open('assets/logdata.txt', 'r') as file:
for line in file:
match = re.match(pattern, line)
if match:
host, user_name, time, request = match.groups()
user_name = user_name if user_name else '-'
log_items.append({
"host": host,
"user_name": user_name,
"time": time,
"request": request,
})
return log_items
给定验证代码:
assert len(logs()) == 979
one_item = {
"host": "146.204.224.152",
"user_name": "feest6811",
"time": "21/Jun/2019:15:45:24 -0700",
"request": "POST /incentivize HTTP/1.1",
}
assert (
one_item in logs()
), "Sorry, this item should be in the log results, check your formating"
验证失败:
The following cell has changed: assert len(logs()) == 979 one_item = { "host": "146.204.224.152", "user_name": "feest6811", "time": "21/Jun/2019:15:45:24 -0700", "request": "POST /incentivize HTTP/1.1", } assert ( one_item in logs() ), "Sorry, this item should be in the log results, check your formating"