how to filter out lines in text file with substring

Question:

I have a text file with contents like this:

ADUMMY ADDRESS1
BDUMMY MEMBER
AA1400 EL DORA RD
BCARLOS
A509 W CARDINAL AVE
BJASMINE

I want a python script to count the number of lines that begin with "B" but do not contain the substring "DUMMY".

Here is what I have so far but I dont know how to do the filter in the "if" statement.

def countLinesMD(folder,f):
    file = open(folder+f,"r")
    Counter = 0

    # Reading from file
    Content = file.read()
    CoList = Content.split("n")

    for i in CoList:
        if i.startswith("B"):
            Counter += 1
return Counter 

                                                       
Asked By: Ben Smith

||

Answers:

Just add it to the if statement:

def countLinesMD(folder,f):
    file = open(folder+f,"r")
    Counter = 0

    # Reading from file
    Content = file.read()
    CoList = Content.split("n")

    for i in CoList:
        if i.startswith("B") and "DUMMY" not in i.split():
            Counter += 1
return Counter 
Answered By: CodeKorn

I believe you can just do this
if i.startswith("B") and "DUMMY" not in i:

Answered By: Jdes
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.