繁体   English   中英

64位系统,8GB的RAM,超过800MB的CSV并使用python读取会导致内存错误

[英]64 bit system, 8gb of ram, a bit more than 800MB of CSV and reading with python gives memory error

f = open("data.csv")
f.seek(0) 
f_reader = csv.reader(f)
raw_data = np.array(list(islice(f_reader,0,10000000)),dtype = int)

上面是我用来读取csv文件的代码。 csv文件只有大约800 MB,我使用的是64位系统,内存为8GB 该文件包含1亿行。 但是,更不用说读取整个文件了,即使读取前1000万行也给我一个'MemoryError:'<-这确实是整个错误消息。

有人可以告诉我为什么吗? 另外还有一个问题,有人可以告诉我如何朗读吗,比如说2000万行? 我知道我需要使用f.seek(某个数字),但是由于我的数据是一个csv文件,所以我不知道应该将哪个数字确切地放入f.seek()中,以便它从第20行开始精确读取。

非常感谢你。

有人可以告诉我如何阅读,例如说第2千万行吗? 我知道我需要使用f.seek(一些数字)

不,在这种情况下,您不能(也不能)使用f.seek() 相反,您必须以某种方式读取前两千万行中的每一行。

Python文档具有以下配方:

def consume(iterator, n):
    "Advance the iterator n-steps ahead. If n is none, consume entirely."
    # Use functions that consume iterators at C speed.
    if n is None:
        # feed the entire iterator into a zero-length deque
        collections.deque(iterator, maxlen=0)
    else:
        # advance to the empty slice starting at position n
        next(islice(iterator, n, n), None)

使用它,您将因此开始经过20,000,000行:

#UNTESTED
f = open("data.csv")
f_reader = csv.reader(f)
consume(f_reader, 20000000)
raw_data = np.array(list(islice(f_reader,0,10000000)),dtype = int)

也许这可能会更快:

#UNTESTED
f = open("data.csv")
consume(f, 20000000)
f_reader = csv.reader(f)
raw_data = np.array(list(islice(f_reader,0,10000000)),dtype = int)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM