Question

Im reading a txt-file, and counting how many times specific words are in the file. For example I want to know how many times "ABC" and "DEF" are there. My first idea was:

for line in open:
    if "ABC" or "DEF" in line:
        x=x+1

the result is x= 33... but thats wrong. Using:

for line in open:
    if "ABC" in line:
        x=x+1
    if "DEF" in line:
        x=x+1

the result is 8! and thats correct.

Why the first example doesnt work?

Was it helpful?

Solution

"ABC" or "DEF" in line

is evaluated as

("ABC") or ("DEF" in line)

Since "ABC" is a truthy value, the if condition will be satisfied always. You can fix this problem like this

if "ABC" in line or "DEF" in line:

which will be evaluated like

("ABC" in line) or ("DEF" in line)

So, if the line had either ABC or DEF, the condition will be satisfied.

If the number of valid words to be checked is large, it would be inconvenient to write a lengthy if condition. Instead of that, you should write that condition, with any function, like this

valid_words = ["ABC", "DEF"]
...
if any(word in line for word in valid_words):

Suggestions:

  1. Avoid using open as a variable name, as it will shadow the builtin open function

  2. When you are dealing with files, make sure that you close the opened files properly or prefer to use with statement.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top