尝试在大型 csv 文件上进行查找和替换时如何处理 MemoryError?

How to deal with MemoryError when trying to make find and replace on a big csv file?

我是初学者,在我的代码中遇到 MemoryError 问题。 csv 文件很大 (1,5gb),我想查找每个 " 字符并将其替换为空白 space。代码正在处理较小的文件,但在此文件上出现 return MemoryError。我在这里发现我可以使用 Garbage Colector 并试图做到这一点但失败了。我该如何解决这个问题?

text = open(r"C:\Users\jarze\abc.csv", "r")
text = ''.join([i for i in text]) \
    .replace('"', '')
x = open(r"C:\Users\jarze\abc.csv","w")
x.writelines(text)
x.close()

已针对一般情况回答此问题here

总而言之,python 的文件对象已经是一个生成器,并被定义为一种逐行读取文件的内存高效方式(参见 here):

f_out = open(r"C:\Users\jarze\out_file.csv","w")

with open(r"C:\Users\jarze\in_file.csv", "r") as f_in:
    ''' 
    As Tomerikoo indicates, This is a preferred way of opening 
    files in python and you don't need to close it later.
    '''
    for line in f_in:
        f_out.write(line.replace('"', ''))    

f_out.close()