Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/345.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
我需要帮助-python,我不需要';我不理解它,使用findall函数,没有给出错误_Python_Regex - Fatal编程技术网

我需要帮助-python,我不需要';我不理解它,使用findall函数,没有给出错误

我需要帮助-python,我不需要';我不理解它,使用findall函数,没有给出错误,python,regex,Python,Regex,程序运行时没有错误,我的文件checker.txt与.py文件在同一个文件中,shell窗口中没有输出任何内容,没有“它起作用”(无关数据=im使用aircrack ng输出到文本文件中,然后用python读取文本文件,然后打开LED灯) 正如Cyber所评论的,返回匹配字符串的列表(或列表取决于捕获组的数量) 如果列表包含任何项(否则为假值),则将其视为真值。所以只需将其用作谓词 .... search = findall("2K([A-Z]* [A-Z]*)! ", file2) if se

程序运行时没有错误,我的文件checker.txt与.py文件在同一个文件中,shell窗口中没有输出任何内容,没有“它起作用”(无关数据=im使用aircrack ng输出到文本文件中,然后用python读取文本文件,然后打开LED灯)

正如Cyber所评论的,返回匹配字符串的列表(或列表取决于捕获组的数量)

如果列表包含任何项(否则为假值),则将其视为真值。所以只需将其用作谓词

....
search = findall("2K([A-Z]* [A-Z]*)! ", file2)
if search:
    booleanvar = True
    print "it worked"
search = re.search("2K([A-Z]* [A-Z]*)! ", file2)
if search:
    booleanvar = True
    print "it worked"
如果不需要所有匹配的字符串,而只检查它是否匹配至少一个子字符串,请使用which返回匹配的对象或
None
。(当用作谓词时,匹配的对象也被视为真值)


非常感谢大家,我忘了findall列了一个结果清单。如果搜索[0]==(我想要的),我只是简单地更改了它,它工作了:D


再次感谢

findall
返回一个
列表
。因此,任何列表都不会等于字符串literal
“KEY FOUND”
。此外,将
booleanvar
重命名为
FOUND
,并在未找到时写入
;可读性很重要。
search = re.search("2K([A-Z]* [A-Z]*)! ", file2)
if search:
    booleanvar = True
    print "it worked"