【发布时间】:2019-07-12 16:21:36
【问题描述】:
试图从读取的文件中获取特定行,并使其成为返回的可用变量。
有关文件中数据的一些信息。语法是这样的。
A line of text I do not need
New domain: www.example.com
Another line that I do not need
New domain: www.example2.com
Ect...
它读取文件,我已经尝试了示例正则表达式模式的一系列变体,并且知道我很接近。除此之外,它相当简单。
data = []
with open('test.txt', 'r') as file:
data = (re.findall(r"(?<=New domain:).+$",open('test.txt'), re.M))
return data
快乐之路: 该函数从 test.txt 文件中读取,仅查看以 New domain: 开头的行,并且仅将 url 一直到行尾并将其放入列表中。
错误: 它只是告诉我模式语法是错误的。
【问题讨论】:
-
你可以尝试去掉
$和re.M。 -
另外,findall 不是在那里寻找字符串。是否打开返回文件句柄?