I use csv.DictReader
to read data from a CSV file. When the reader is iterated over, it yields dictionaries with keys taken from the CSV header and the values per row:
with open(filename) as h:
data = csv.DictReader(h)
for row in data:
# row is dict
Each row is a dictionary with keys, and each row has exactly the same keys. In case when the values are integers and the keys (strings) are long, the keys occupy more memory space than the values..
Can I iterate the rows in the way that the keys of each row point to the same instance of keys, so I save memory space per row?
Note that I don't know the keys in advance - they are taken from CSV header. Otherwise I could use namedtuple
or __slots__