- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我在 Coursera 上学习这门类(class),在做第一个作业时遇到了一些问题。任务基本上是使用正则表达式从给定文件中获取某些值。然后,该函数应输出包含这些值的字典:
example_dict = {"host":"146.204.224.152",
"user_name":"feest6811",
"time":"21/Jun/2019:15:45:24 -0700",
"request":"POST /incentivize HTTP/1.1"}
这只是文件的屏幕截图。由于某些原因,如果不是直接从 Coursera 打开,链接将不起作用。对于格式错误,我提前表示歉意。我必须指出的一件事是,在某些情况下,正如您在第一个示例中看到的那样,没有用户名。而是使用“-”。
159.253.153.40 - - [21/Jun/2019:15:46:10 -0700] "POST /e-business HTTP/1.0" 504 19845
136.195.158.6 - feeney9464 [21/Jun/2019:15:46:11 -0700] "HEAD /open-source/markets HTTP/2.0" 204 21149
这就是我目前拥有的。但是,输出为无。我想我的模式有问题。
import re
def logs():
with open("assets/logdata.txt", "r") as file:
logdata = file.read()
# YOUR CODE HERE
pattern = """
(?P<host>\w*)
(\d+\.\d+.\d+.\d+\ )
(?P<user_name>\w*)
(\ -\ [a-z]+[0-9]+\ )
(?P<time>\w*)
(\[(.*?)\])
(?P<request>\w*)
(".*")
"""
for item in re.finditer(pattern,logdata,re.VERBOSE):
print(item.groupdict())
最佳答案
您可以使用以下表达式:
(?P<host>\d+(?:\.\d+){3}) # 1+ digits and 3 occurrenses of . and 3 digits
\s+\S+\s+ # 1+ whitespaces, 1+ non-whitespaces, 1+ whitespaces
(?P<user_name>\S+)\s+\[ # 1+ non-whitespaces (Group "user_name"), 1+ whitespaces and [
(?P<time>[^\]\[]*)\]\s+ # Group "time": 0+ chars other than [ and ], ], 1+ whitespaces
"(?P<request>[^"]*)" # ", Group "request": 0+ non-" chars, "
参见 regex demo .查看Python demo :
import re
logdata = r"""159.253.153.40 - - [21/Jun/2019:15:46:10 -0700] "POST /e-business HTTP/1.0" 504 19845
136.195.158.6 - feeney9464 [21/Jun/2019:15:46:11 -0700] "HEAD /open-source/markets HTTP/2.0" 204 21149"""
pattern = r'''
(?P<host>\d+(?:\.\d+){3}) # 1+ digits and 3 occurrenses of . and 3 digits
\s+\S+\s+ # 1+ whitespaces, 1+ non-whitespaces, 1+ whitespaces
(?P<user_name>\S+)\s+\[ # 1+ non-whitespaces (Group "user_name"), 1+ whitespaces and [
(?P<time>[^\]\[]*)\]\s+ # Group "time": 0+ chars other than [ and ], ], 1+ whitespaces
"(?P<request>[^"]*)" # ", Group "request": 0+ non-" chars, "
'''
for item in re.finditer(pattern,logdata,re.VERBOSE):
print(item.groupdict())
输出:
{'host': '159.253.153.40', 'user_name': '-', 'time': '21/Jun/2019:15:46:10 -0700', 'request': 'POST /e-business HTTP/1.0'}
{'host': '136.195.158.6', 'user_name': 'feeney9464', 'time': '21/Jun/2019:15:46:11 -0700', 'request': 'HEAD /open-source/markets HTTP/2.0'}
关于python - Coursera类(class)-Python Assignment 1中的数据科学介绍,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64425164/
使用 Mac 操作系统 10.10.3RStudio 版本 0.98.1103 我的工作目录是一个包含 332 个 .csv 文件的列表,我的设置是正确的。这是代码: pollutantmean <-
我在 coursera 上的 scala 中参加了 martin odersky 的函数式编程类(class)。 但是,我无法理解第二个作业 Funsets.scala 的解决方案。 type Set
我正在上一门算法课,现在我们正在学习贪婪算法。我的两个解决方案在某些测试用例上输出“未知信号 11”。 但是,我用尽可能大的输入将我的程序推向了极限。它在我的电脑上运行得很好。然而,在 Courser
我在 Coursera 上问了这个问题,但没有人回复,所以我来到这里。这是关于 Scala 函数式编程原理类(class)的最后一个作业(Anagrams)。 如果函数减法返回无序出现,Anagram
我想通过这些链接后面的程序(主要是 Python)提取 Coursera 视频下载链接 https://www.coursera.org/learn/human-computer-interactio
我正在使用 Coursera 的 jupyterNotebook,但没有办法将所有内容恢复到开头。 唯一相关的选项似乎是“恢复到检查点”——但我一开始没有保存检查点。 这是否意味着我无法恢复到它? 最
关闭。这个问题不满足Stack Overflow guidelines .它目前不接受答案。 想改善这个问题吗?更新问题,使其成为 on-topic对于堆栈溢出。 2个月前关闭。 Improve th
我正在尝试从 OS X Mavericks 上的命令行运行 coursera-dl。 控制台输出如下: $ coursera-dl Traceback (most recent ca
来自那门类(class) https://class.coursera.org/progfun-004/assignment 我下载了 http://spark-public.s3.amazonaws
我目前正在为 coursera 上提供的创业工程类(class)学习 program 2 我正在使用 Amazon Web 服务使用 ubuntu 实例进行编程,但我的编程一直挂起。我的 node.j
在完成“Scala 中的函数式编程原则”@coursera 类(class)第 3 周的作业时,我发现当我实现视频类(class)中所示的函数联合时: override def union(tha
我正在尝试从 Julia 中的 Coursera 进行逻辑回归,但它不起作用。 计算梯度的 Julia 代码: sigmoid(z) = 1 / (1 + e ^ -z) hypotesis(thet
我正在 Coursera 上学习 Andrew Ng 的机器学习,方法是使用 Python 而不是 MATLAB 实现所有代码。 在编程练习 3 中,我以向量化形式实现了正则化逻辑回归成本函数: de
我在理解如何对 Coursera 上提供的机器学习类(class)中的函数进行矢量化时遇到问题。 在类(class)中,Andrew Ng 解释了假设可以向量化为 theta 乘以 x 的转置: H(
我从 MOOC 中学到了很多东西,所以我想为此返回一些东西我正在考虑在 kivy 中设计一个小应用程序,因此需要 python 实现,实际上我想要实现的是记录通过程序进入我的 Coursera 帐户并
任务 -- 此代码问题的目标是实现二分查找算法。 输入格式 -- 输入的第一行包含一个整数 n 和一个序列 a0 &a, long long x) { size_t left = 0, righ
当我尝试在 Angular 中使用 jsonp 方法请求 coursera api 时,出现以下错误。 Refused to execute script from 'https://api.cour
我正在用 C# 实现 Ng 的 OCR 神经网络示例。我认为我已经正确实现了所有公式[矢量化版本],并且我的应用程序正在训练网络。 关于如何看到我的网络在识别方面的改进,有什么建议吗 - 无需在训练完
我已经开始 Coursera Algorythms course .练习作业必须在Java上完成,他们建议使用DrJava作为IDE,但这真的很不方便。所以我想使用 Eclipse,但问题是我无法使用
我正在 coursera java 类(class)中做作业。作业是关于java中的统计数据,他们给了我一些文件,其中包含每个名字的出生数量,并想要每个名字的排名,例如hesham 15,ahmed
我是一名优秀的程序员,十分优秀!