我的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 中有字符串。如果相同则应打印 PASS。
如果我运行这个程序,我不会收到任何错误。如果我打印“match2”和“mat2”则给出相同的相等输出。但如果我使用“if match2 == mat2”则不会打印为“PASS”。
任何人都可以帮我解决这个问题吗?
提前致谢。
谢谢
库马尔。
最佳答案
re.findall
返回一个列表,而不是字符串。所以 mat2
也应该是一个列表:
mat2 = ['your test contains errors']
如果您想检查字符串中您的测试是否包含错误
,您可以使用in
运算符:
if "your test contains errors" in output:
print "PASS"
关于python - 如何在python中使用IF语句匹配两个相等的字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32679530/