Upgrading CSV from a DictReader to a namespace 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. We can also 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 reader, we must use row[0]
, the semantics of this are completely obscure. When we use a DictReader
, we can use row['date']
, which is less obscure, but is 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
The column names (and the data types for...