Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

how to filter out lines in text file with substring

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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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 

                                                       

>Solution :

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

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading