正确吗?我希望有人能帮助我,呵呵



编写一个Python程序,使用mbox-short.txt文本文件搜索以'F'开头,后面跟着2个字符,后面跟着'm:'的行。编写一个Python程序,搜索以From开头并带有@符号

的行。我代码:

import re
file_hand = open("mbox-short.txt")
for line in file_hand:
line = line.rstrip()
if re.search('From:', line):
print(line)

您的代码似乎缺乏将找到您正在寻找的结果的实际正则表达式。如果我理解正确的话,你的目标是找到以F开头的行,后面跟着任意两个字符。如果是这种情况,您希望将该行打印到终端。让我来引导你:

import re
file_hand = open("mbox-short.txt")
for line in file_hand:  #NB: After a new scope is entered, use indentation
result = re.search("$f..", line)   #pattern, search string
#$ matches character before the first in a line
#. matches 1 occurence of any character
if result.group() != "":  #access result of re.search with group() method
print(line)

我相信你能听懂。如果你需要大写F,我将把它作为你的家庭作业,让你找出如何做大写F。

你可以在这里练习使用regexp:https://regexr.com/

或阅读更多关于这里:https://www.youtube.com/watch?v=rhzKDrUiJVk

我觉得你的问题问得不够清楚,让每个人都能理解。此外,插入代码以获得更好的可读性("代码示例")。我已经在你的代码中做过了,所以你可以看一下。

最新更新