Question 1.
I tried reading a CSV file with size of ~1GB like below
import csv
res = []
with open("my_csv.csv", "r") as f:
reader = csv.reader(f)
for row in reader:
res.append(row)
I thought 1GB is small enough to load it on my memory as a list. But in fact, the code freezes and the memory usage was 100%. I had checked the extra memory was few GB before I ran the code.
This answer says,
"You are reading all rows into a list, then processing that list. Don't do that."
But I wonder WHY? Why does the list possesses much much bigger memory than the file size?
Question 2.
Is there any method to parse a CSV into a dict without a memory issue?
For example,
CSV
apple,1,2,a
apple,4,5,b
banana,AAA,0,3
kiwi,g1,g2,g3
Dict
{"apple" : [[1, 2, a], [4, 5, b]],
"banana": [[AAA, 0, 3]],
"kiwi" : [[g1, g2, g3]]}