jv95
jv95

Reputation: 691

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? I want it to return 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")

Upvotes: 0

Views: 60

Answers (1)

rahlf23
rahlf23

Reputation: 9019

You are passing a generator object to your if statement, for which the boolean evaluation is True:

bool((s in line for s in fields_to_find))

Returns:

True

Instead, IIUC you can use any() and pass your generator:

any(s in line for s in fields_to_find)

Returns:

False

Upvotes: 1

Related Questions