正如其他人所说,最明显的解决方案是使用熊猫读csv!
该方法有一个名为skiprows的参数:
skiprows:list-like,int或callable,可选
文件开始时要跳过的行号(0索引)或要跳过的行数(int)。在
如果可调用,将根据行索引计算可调用函数,如果应跳过该行,则返回True;否则返回False。一个有效的可调用参数的例子是[0,2]中的lambda x:x。
你可以这样做:import pandas as pd
data = pd.read_csv('path/to/your/file', skiprows =lambda x: x not in range(50, 60))
由于您指定内存是您的问题,因此可以使用this tutorial中所述的chunksize参数
他说:The parameter essentially means the number of rows to be read into a
dataframe at any single time in order to fit into the local memory.
Since the data consists of more than 70 millions of rows, I specified
the chunksize as 1 million rows each time that broke the large data
set into many smaller pieces.
^{pr2}$
您可以尝试这样做,然后在块上迭代,只检索要查找的行。在
如果行号在指定列表中,则函数应返回true