Python初学问题

菜鸟请教,问题到底出在哪呢?

给定题目:

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"
  • 17
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值