python - 如何计算仅包含一个逗号分隔的行数并忽略计算文本文件中两个逗号分隔的行数
问题描述
我想计算仅包含一个逗号分隔的行数并忽略计算文本文件中两个逗号分隔的行数
下面是我的代码
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
推荐阅读
- c - ALSA 在看似正确的时间安排下出人意料地不足
- python - 将字符串中的所有数字移到末尾
- mysql - DDL的事务日志
- javascript - JavaScript - 复制数组不起作用
- docker - 使用 tensorflow docker 映像在已安装的驱动器上启动 jupyter lab
- javascript - 如何处理输入字符串的错误以不允许数字或符号
- intellij-idea - 在项目 IntelliJ IDEA 中查找最长的文件
- python - PyCharm 中的“未解析的属性引用”
- mysql - Spring Boot:Jdbc javax.net.ssl.SSLException:在接收对等方的 close_notify 之前关闭入站
- arrays - 'numpy.ndarray' 对象不可调用?