簡體   English   中英

在Python中處理文件時出現內存錯誤

[英]Memory error when processing files in Python

我打算根據每一行的密鑰將一個總共約500MB的文件讀入字典。 代碼片段如下:

f2 = open("ENST-NM-chr-name.txt", "r")   # small amount
lines = [l.strip() for l in f2.readlines() if l.strip()]
sample = dict([(l.split("\t")[2].strip("\""), l) for l in lines])    ## convert [(1,2), (3,4)] to {1:2, 3:4}

在內存為4GB的計算機上運行時,python會抱怨內存錯誤。 如果我將sample變量的評估表達式更改為[l for l in lines]則可以正常工作。

起初,我認為這是由於split方法占用了大量內存,因此我將代碼調整為:

def find_nth(haystack, needle, n):
    start = haystack.find(needle)
    while start >= 0 and n > 1:
        start = haystack.find(needle, start+len(needle))
        n -= 1
    return start

...

sample = dict([(l[find_nth(l, "\t", 4):].strip(), l) for l in lines])

但是事實證明是一樣的。

一個新的發現是,如果我刪除dict()轉換而不管代碼邏輯如何,它將在沒有OOM的情況下正常運行。

誰能給我一些關於這個問題的想法?

你創建了一個包含每一行,這將繼續存在,直到列表lines超出范圍,然后創建基於關閉它完全不同的字符串的另一大名單,那么dict掉的那個,才可以走出去的記憶。 只需一步就可以構建該dict

with open("ENST-NM-chr-name.txt") as f:
    sample = {}

    for l in f:
        l = l.strip()

        if l:
            sample[l.split("\t")[2].strip('"')] = l

通過使用生成器表達式而不是列表推導,您可以達到大致相同的效果,但是(對我而言)不strip兩次感覺更好。

如果將列表變成生成器,而字典變成漂亮的字典理解 ,該怎么辦:

f2 = open("ENST-NM-chr-name.txt", "r")   # small amount
lines = (l.strip() for l in f2 if l.strip())
sample = {line.split('\t')[2].strip('\"'): line for line in lines}

上面的第2 lines = (l.strip() for l in f2.readlines() if l.strip())錯誤地是lines = (l.strip() for l in f2.readlines() if l.strip())

生成器和dict理解是否可以(以某種方式)減輕內存需求?

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM