user16345032
user16345032

Reputation: 31

To count the number of lines only when particular characters are present - Python

I am having a text file(consider it as main.txt) which has multiple language contents and I am having a charcter set text file which has particular characters. For example: character set text file contains

e
f
g
h

I want to count the number of lines in main.txt which contains anyone of the character in the character set. If a line in main.txt contains e or f or g or h, then that line should be counted as 1.

Upvotes: 0

Views: 165

Answers (1)

Nicolas Quintana
Nicolas Quintana

Reputation: 160

count = 0
with open('main.txt') as f:
    for line in f:
        if "e" in line or "f" in line or "g" in line or "h" in line:
            count = count + 1
print(count)

Upvotes: 0

Related Questions