如何统计文本文件中只包含一个逗号分隔的行数,忽略统计两个逗号分隔的行数



我想计算文本文件中只包含一个逗号分隔的行数,而忽略包含两个逗号分隔的行数

下面是我的代码
def countLines(filename):
with open(filename,'r') as f: 
CoList = f.split("n") 
Counter = 0 
for i in CoList: 
if i: 
Counter += 1 
return CoList
countLines('demo.txt')

我想这就是你想要的?

def countLines(filename):
with open(filename,'r') as f: 
Counter = 0 
for line in f: 
if line.count(',') == 1:
Counter += 1 
return Counter

最新更新