[英]how to filter out lines in text file with substring
我有一個包含如下內容的文本文件:
ADUMMY ADDRESS1
BDUMMY MEMBER
AA1400 EL DORA RD
BCARLOS
A509 W CARDINAL AVE
BJASMINE
我想要一個 python 腳本來計算以“B”開頭但不包含 substring“DUMMY”的行數。
到目前為止,這是我所擁有的,但我不知道如何在“if”語句中進行過濾。
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
我相信if i.startswith("B") and "DUMMY" not in i:
只需將其添加到if
語句中:
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
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.