我的Python代码:

import re

output = "your test contains errors"

match2 = re.findall('(.* contains errors)',output)
mat2 = "['your test contains errors'] "

if match2 == mat2:
    print "PASS"

在上面的python程序中,我在'match2'和mat2中有字符串。如果它是一样的,它应该打印通行证。
如果我运行这个程序,我不会得到任何错误。如果我打印“match2”和“mat2”的输出相同。但如果我使用“if match2==mat2”不是打印为“PASS”。
谁能帮我修一下这个吗。
提前谢谢。
谢谢,
库马尔。

最佳答案

re.findall返回一个列表,而不是字符串。所以mat2也应该是一个列表:

mat2 = ['your test contains errors']

如果要检查字符串中的your test contains errors,可以使用in运算符:
if "your test contains errors" in output:
    print "PASS"

09-03 19:51