Skip to main content
 首页 » 编程设计

python之使用python计算csv文件中的逗号

2025年05月04日9pander-it

我正在尝试逐行计算 .csv 文件中的逗号。不幸的是,它总是归零。

    import csv  
 
    with open('Test.csv') as csv_file: 
       csv_reader = csv.reader(csv_file, delimiter=',') 
       for row in csv_reader: 
           numCommas = row.read().count(',') 
           print numCommas 

但我总是得到 0。

请您参考如下方法:

如果你只是想计算逗号而不需要数据,则不需要csv模块:

with open('Test.csv', 'r') as csv_file: 
    for line in csv_file: 
        print(line.count(','))