python - 计算文件夹中每个 csv 文件的行数
问题描述
如何计算文件夹中每个 csv 文件的行数?
with open('filename.csv', 'r', encoding="latin-1") as csvfile:
readCSV=csv.reader(csvfile, delimiter=',')
row_count=sum(1 for row in readCSV)
print(row_count)
for row in readCSV:
print(row[1])
我为一个文件尝试了此操作,但我想为每个文件执行此操作,并且有很多..
import os
a ="foldername"
os.listdir(a)
我试过这个,但我不知道它是怎么回事..我对python真的很陌生..
非常感谢。
解决方案
假设文件放在一个文件夹中:
import os
path = '/some/path/to/file'
for filename in os.listdir(path):
with open(filename, 'r', encoding="latin-1") as fileObj:
# -1 to exclude the header
print("Rows Counted {} in the csv {}:".format(len(fileObj.readlines()) - 1, filename))
输出(已测试):
Rows Counted 198 in the csv celebList.xlsx:
Rows Counted 148 in the csv cel_lis.xls:
推荐阅读
- c# - Ftp 连接最佳实践
- javascript - 基于 web 的 firebase 代码在执行时运行多次
- python - seetings.py 或 docker 中的数据库配置错误
- wordpress - 在 WooCommerce 中创建自定义字段时,如何将单选按钮选项排列在一行中?
- android - 我需要一个弧向量来自定义折叠工具栏
- python-3.x - 我应该选择哪些凭证以及如何选择?
- python - Google Assistant on Raspberry Pi 3 Segmentation fault
- c# - Python 中 C# MessageDescriptor Parser 的 gRPC 模拟
- spring-boot - Jwt 令牌在每次重新启动 Spring Boot 应用程序时都会失效
- elixir - 第二个 HTTPoison.get 请求返回 404