Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/320.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用Python(200 GB+;)从长csv文件的中间读取块_Python_Pandas_Csv_Large Data_Chunks - Fatal编程技术网

如何使用Python(200 GB+;)从长csv文件的中间读取块

如何使用Python(200 GB+;)从长csv文件的中间读取块,python,pandas,csv,large-data,chunks,Python,Pandas,Csv,Large Data,Chunks,我有一个大的csv文件,我正在读它与块。在进程的中间,内存已经满了,所以我想从它离开的地方重新启动。我知道哪个区块,但不知道如何直接进入该区块 这就是我试过的 # data is the txt file reader = pd.read_csv(data , delimiter = "\t", chunksize = 1000 ) # Pleas

我有一个大的csv文件,我正在读它与块。在进程的中间,内存已经满了,所以我想从它离开的地方重新启动。我知道哪个区块,但不知道如何直接进入该区块

这就是我试过的

# data is the txt file
reader = pd.read_csv(data , 
                     delimiter = "\t",
                     chunksize = 1000
                    )


# Please see the code below. When my last process broke, i was 154 so I think it should 
# start from 154000th line. This time I don't 
# plan to read whole file at once so I have an 
# end point at 160000

first = 154*1000
last = 160*1000

output_path = 'usa_hotspot_data_' + str(first) + '_' + str(last) + '.csv'
print("Output file: ", output_path)

try:
    os.remove(output_path)
except OSError:
    pass

# Read chunks and save to a new csv
for i,chunk in enumerate(reader):
    if (i >= first and i<=last) :
          < -- here I do something  -- > 
        # Progress Bar to keep track 
        if (i% 1000 == 0):
            print("#", end ='')
#数据是txt文件
读卡器=pd.read\U csv(数据、,
分隔符=“\t”,
chunksize=1000
)
#请参阅下面的代码。当我的最后一个进程中断时,我是154岁,所以我认为应该是这样
#从154000线开始。这次我不知道
#计划一次读取整个文件,这样我就有一个
#终点16万
第一个=154*1000
最后一次=160*1000
输出路径='美国热点数据'+str(第一个)+''+str(最后一个)+'.csv'
打印(“输出文件:”,输出路径)
尝试:
删除操作系统(输出路径)
除操作错误外:
通过
#读取块并保存到新的csv
对于i,枚举中的块(读取器):
如果(i>=first和i
#要跟踪的进度条
如果(i%1000==0):
打印(“#”,结束=”)
然而,这需要花费很多时间才能到达我想去的第I行。我怎样才能跳过阅读之前的区块,直接去那里呢?

skiprows:要跳过的行号(0索引)或要跳过的行数 (int)在文件的开头


您可以将此skiprows传递给
read_csv
,它的作用类似于偏移量。

dask可以在此处充当救世主。请参阅您可以使用skiprows参数: