csv文件中的行计数 [英] Row count in a csv file

查看:138
本文介绍了csv文件中的行计数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可能犯了一个愚蠢的错误,但我可以找到它在哪里。我要计算我的csv文件中的行数。我写了这个,显然aint工作:我hav row_count = 0,而它应该是400.欢呼。

I am probably making a stupid mistake but I can t find where it is. I wan to count the number of line in my csv file. I wrote this, and obviously aint working : I hav row_count = 0 while it should be 400. Cheers.

f = open(adresse,"r")
reader = csv.reader(f,delimiter = ",")
data = [l for l in reader]
row_count = sum(1 for row in reader)

print row_count


推荐答案

with open(adresse,"r") as f:
    reader = csv.reader(f,delimiter = ",")
    data = list(reader)
    row_count = len(data)

您尝试读取文件两次,指针在保存数据列表后已经到达文件结尾。

You are trying to read the file twice, when the file pointer has already reached the end of file after saving the data list.

这篇关于csv文件中的行计数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆