我假设我收到此错误,因为我正在读同一个CSV两次(我不完全理解为什么这是一个问题)但我需要下面的代码来做以下两件事..
1)为我提供CSV中所有行的总计数。
2)给我第一个两行数据,供我显示给用户。
file = upload.filepath
#I READ THE FILE
file_read = csv.reader(file)
#GET THE COUNT I.E. 100 ROWS
row_count = sum(1 for row in file_read)
#ADD TO DATA JUST THE FIRST TWO ROWS THAT I WILL USE TO DISPLAY BACK TO THE USER
data = []
for i in range(2):
data.append(file_read.next())
我怎样才能做到这一点?
答案 0 :(得分:4)
使用islice
即可:
from itertools import islice
import csv
with open('somefile') as fin:
csvin = csv.reader(fin)
firstn = list(islice(csvin, 2))
rows = len(firstn) + sum(1 for row in csvin)
这样可以节省您必须阅读文件然后寻找开始,并正确返回行数,然后文件将自动关闭或者在异常情况下关闭。
答案 1 :(得分:3)
为什么不扭转这两个陈述?您可以使用以下内容阅读前两行:
#ADD TO DATA JUST THE FIRST TWO ROWS THAT I WILL USE TO DISPLAY BACK TO THE USER
data = []
for i in range(2):
data.append(file_read.next())
然后对剩余的行进行行计数,并添加2:
#GET THE COUNT I.E. 100 ROWS
row_count = 2 + sum(1 for row in file_read)
但是,如果您有< 2行,则会给出不正确的结果。更好的解决方案可能是简单地读入整个文件,然后计算结果列表中的行。
import csv
# Open the file upload.filepath, get a pointer in file
# file automatically closed at end of 'with' context
with open(upload.filepath, 'r') as file:
#I READ THE FILE
file_read = csv.reader(file)
data = []
for row in file_read:
data.append(row)
rows = len(data)
first_two_rows = data[:2]
如果它是一个庞大的文件,并且你担心内存,你可以添加一个count变量,只有在< = 2
时附加到数据data = []
rows = 0
for row in file_read:
rows += 1
if rows<3:
data.append(row)
答案 2 :(得分:1)
您应该执行以下操作:
f = open('test.csv')
reader = csv.reader(f)
row_count = sum(1 for each in reader)
f.seek(0) #reposition
data = []
for i in range(2):
data.append(reader.next())
f.close()