I have a large text file (~450MB -> 129,000 lines and 457,000,000 chars) , when I try to work with this file after some while , Memory Error
rises , here is my code:
docDict = {}
ind = 1
with open('somefile.txt',encoding='utf-8') as f:
for line in f:
data = line.split(' ')
docDict[ind] = data
ind+=1
I saw this , but I read file line by line.
The
memory error
is raising here because even if you read your file line by line, you store its content in your dictionarydocDict
, so in the memory.I don't know what you plan to do with this dictionary, but I recommend to do the process after each line you read, and then store the result in a variable (if the process compresses a lot), or directly in a file or database.
Hope I helped ! See ya !