我需要python的帮助,我不懂,使用findall函数,没有给出错误

2024-06-17 11:59:17 发布

您现在位置:Python中文网/ 问答频道 /正文

程序运行没有错误,我有文件棋盘格.txt在与.py文件相同的文件中,shell窗口中没有输出任何内容,没有“it worked”(unrelated data=im使用aircrack ng输出到文本文件中,然后用python读取文本文件,然后打开LED灯)

from re import findall
booleanvar = False
while(booleanvar == False):
    file1 = open("checker.txt", "U")
    file2 = file1.read()
    file1.close
    search = findall ("2K([A-Z]* [A-Z]*)! ", file2)
    if(search == "KEY FOUND"):
        booleanvar = True
        print "it worked"

Tags: 文件pytxt程序运行falsesearch棋盘错误
2条回答

非常感谢大家,我忘了芬德尔列了一个结果清单。我只是简单地改变了search[0]==(我想要的)并且成功了:D

再次感谢

正如赛博所评论的,^{}返回匹配字符串的列表(或者列表取决于捕获的组的数量)。你知道吗

如果列表包含任何项(否则为假值),则将其视为真值。所以就把它当作谓词。你知道吗

....
search = findall("2K([A-Z]* [A-Z]*)! ", file2)
if search:
    booleanvar = True
    print "it worked"

如果不需要所有匹配的字符串,而只需要检查它是否匹配至少一个子字符串,那么使用^{}返回匹配的对象或None。(匹配的对象在用作谓词时也被视为真值)。你知道吗

search = re.search("2K([A-Z]* [A-Z]*)! ", file2)
if search:
    booleanvar = True
    print "it worked"

相关问题 更多 >