Python内存错误。程序占用大量内存

2024-09-27 07:35:46 发布

您现在位置:Python中文网/ 问答频道 /正文

这是我的代码:

import re

h_file_name="testinp.txt"     #This file is very large approx 64GB size
m_file_name="testinpmap.txt"  #This files size is between 25-30MB

with open(h_file_name, "rb") as f, open(h_file_name+".re.txt", "wb+") as f1, open(m_file_name,"rb") as f2:


    req=""
    s=""
    cols1 = []

    for line in f:
            cols= re.split('[ ]+|[ ]+', line.strip())
            for i in range(len(cols)):
            if str(cols[i]).strip() == "1":
                    cols[i] = "2"
            cols1.append(cols)


    for line2 in f2:

            cols2 = re.split('[ ]+|[    ]+', line2.strip())

            if (cols2[3].strip() != "."):
                    for j in range(len(cols1[i])):
                            if str(cols1[i][j]).strip() == "0":
                                cols1[i][j] = "1"


    for c in cols1:
            req += " ".join(map(str,c))
            req += "\n"

    f1.write(req)

你可以看到输入文件非常大,但是我不明白的是,我正在逐行读取文件,那我为什么得到 ““

Traceback (most recent call last): File "re_13.py", line 37, in req += " ".join(map(str,c)) MemoryError"

或者为什么程序从代码执行开始就占用了太多的RAM内存?如何修改脚本以使其占用最小内存且不会耗尽内存?在


Tags: nameinretxtforifasline

热门问题