Using namedtuples field names for column indices in a list of lists
python at deborahswanson.net
Sun Jan 8 06:54:36 EST 2017
Peter Otten wrote, on January 08, 2017 3:01 AM
> Deborah Swanson wrote:
> > to do that is with .fget(). Believe me, I tried every > possible way
> > use instance.A or instance and no way could I get ls[instance.A].
> Sorry, no.
I quite agree, I was describing the dead end I was in from peeling the
list of data and the namedtuple from the header row off the csv
separately. That was quite obviously the wrong path to take, but I
didn't know what a good way would be.
> To get a list of namedtuple instances use:
> rows = csv.reader(infile)
> Record = namedtuple("Record", next(rows))
> records = [Record._make(row) for row in rows]
This is slightly different from Steven's suggestion, and it makes a
block of records that I think would be iterable. At any rate all the
data from the csv would belong to a single data structure, and that
seems inherently a good thing.
a = records[i].A , for example
And I think that this would produce recognizable field names in my code
(which was the original goal) if the following works:
records is the header row == ('Description', 'Location', etc.)
If I can use records[i].Location for the Location column data in row
'i', then I've got my recognizable-field-name variables.
> If you want a column from a list of records you need to
> extract it manually:
> columnA = [record.A for record in records]
This is very neat. Something like a list comprehension for named tuples?
Thanks Peter, I'll try it all tomorrow and see how it goes.
PS. I haven't forgotten your defaultdict suggestion, I'm just taking the
suggestions I got in the "Cleaning up Conditionals" thread one at a
time, and I will get to defaultdict. Then I'll look at all of them and
see what final version of the code will work best with all the factors
More information about the Python-list