I am dealing with big text files that at some point won't fit in to my memory. My idea was to read them line by line (because you don't have to load the whole file in memory) so I won't have memory issues.
with open(file) as infile:
for line in infile:
# do stuff with this line
The issue comes because the way I create those files is with loading dictionaries in them:
def save_dictionary_to_txt_file(dic, file_name):
import json
with open(file_name, 'w') as fp:
fp.write(json.dumps(dic))
So let's say that the dictionary I have is this one:
{'apple': ['5', '65'], 'blue': ['9', '10', '15', '43'], 'candle': ['15'], 'is': ['5', '6', '13', '45', '96']}
Then in the text file I have it in the exact same format as a line. Is there any way in which I could insert new lines in the text file without needing to parse the file again? or most important loading it in memory? I tried with pickle since it is a object serialiation module but didn't have any success. What I tried was to have like the '\n' new line character inside my dictionary every couple of keys.
{'apple': ['5', '65'], 'blue': ['9', '10', '15', '43'], '\n', 'candle': ['15'], 'is': ['5', '6', '13', '45', '96']}
I have tried with json files and it works (keeping the format of the dictionary) but json files have other issues that prevent me from using them in my algorithm. (for example you can't append another dictionary to a json file, or you can if you can load the whole file in memory which I can't do).