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
推荐答案
with open(adresse,"r") as f:
reader = csv.reader(f,delimiter = ",")
data = list(reader)
row_count = len(data)
当保存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屋!
查看全文