Upgrading CSV from DictReader to namedtuple reader
When we read data from a CSV format file, we have two general choices for the resulting data structure:
- When we use
csv.reader()
, each row becomes a simple list of column values. - When we use
csv.DictReader
, each row becomes a dictionary. By default, the contents of the first row become the keys for the row dictionary. The alternative is to provide a list of values that will be used as the keys.
In both cases, referring to data within the row is awkward because it involves rather complex-looking syntax. When we use a csv
reader, we must use row[2]
: the semantics of this are completely obscure. When we use a DictReader
, we can use row['date']
, which is less obscure, but is still a lot of typing.
In some real-world spreadsheets the column names are impossibly long strings. It's hard to work with row['Total of all locations excluding franchisees']
.
What can we do to replace complex syntax with something simpler?
Getting ready
One way to improve the...