[英]get exact match when checking is string is contained is line
I would like to find exact match for items from tuple. 我想找到元组中项目的完全匹配。 Why does my code return True for all lines?
为什么我的代码对所有行都返回True? I want it to return false.
我希望它返回false。 Any ideas?
有任何想法吗? What am I doing wrong?
我究竟做错了什么?
test1.xml -
<field1ff>1</field1ff>
<field1ff>1</field1ff>
<field2ff>1</field2ff>
<field2ff>1</field2ff>
fields_to_find = {"<field1>","<field2>"}
file = open("test1.xml", "r")
for line in file.readlines():
if (s in line for s in fields_to_find):
print("true")
You are passing a generator object to your if statement, for which the boolean evaluation is True
: 您正在将生成器对象传递给if语句,其布尔值评估为
True
:
bool((s in line for s in fields_to_find))
Returns: 返回值:
True
Instead, IIUC you can use any()
and pass your generator: 相反,IIUC可以使用
any()
并传递生成器:
any(s in line for s in fields_to_find)
Returns: 返回值:
False
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.