csv文件中的行计数 [英] Row count in a csv file
本文介绍了csv文件中的行计数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我可能犯了一个愚蠢的错误,但我找不到它在哪里。我想计算我的csv文件中的行数。我写了这个,显然是不工作:我有 row_count = 0
,而它应该是400.欢呼。
I am probably making a stupid mistake, but I can't find where it is. I want to count the number of lines in my csv file. I wrote this, and obviously isn't working: I have 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
reader = csv.reader(f,delimiter =。);解决方法
解决方案
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屋!
查看全文