读取直到Python中的特定CSV行

时间:2014-09-23 09:28:52

标签: python csv

我正在打开一个大的CSV文件,在将数据插入数组时,我得到了MemoryError。那么我该如何读取该CSV文件的特定行(例如从第1行到10000)

这是代码:

datafile=open('test.csv','r')
datareader=csv.reader(datafile,delimiter=';')

for row in datareader:
    MyArray.append(row)

2 个答案:

答案 0 :(得分:2)

使用enumerate()

for i, row in enumerate(datareader):
    MyArray.append(row)
    if i == 10000:
        break

或,对于任何范围:

start = 1000
stop = 2000
for i, row in enumerate(datareader):
    if i < start: 
        continue     # skip this row
    elif i > stop:
        break        # abort the loop
    else:            # ("else" is not strictly necessary here, but more explicit)
        MyArray.append(row)

答案 1 :(得分:2)

我使用islice代替enumerate

from itertools import islice

# First 10000
MyArray.extend(islice(datareader, 10000))

# Or, specify start/stop ranges (10000-20000 (non inclusive))
MyArray.extend(islice(datareader, 10000, 20000))

# Or read in chunks of 10k
for chunk in iter(lambda: list(islice(datareader, 10000)), []):
    # do something with 10k rows