Python中的内存错误。程序需要大量的RAM内存

时间:2018-05-11 07:15:28

标签: python memory-management

所以这是我的代码:

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)

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

  

追踪(最近一次调用最后一次):文件“re_1​​3.py”,第37行,   in req + =“”。join(map(str,c))MemoryError“

或者为什么程序从代码执行开始就占用了太多RAM内存?如何修改脚本以便占用最少的内存并且内存不足?

0 个答案:

没有答案