[Python-ideas] namedtuple literals [Was: RE a new namedtuple]

David Mertz mertz at gnosis.cx
Mon Jul 31 02:27:36 EDT 2017


Yep.  DictRreader is better for my simple example. Just pointing out that
encountering attributes in different orders isn't uncommon.

On Jul 30, 2017 10:55 PM, "Chris Angelico" <rosuav at gmail.com> wrote:

On Mon, Jul 31, 2017 at 3:41 PM, David Mertz <mertz at gnosis.cx> wrote:
>> But most of the time you always have the same attributes in the same
order
>> (think of reading a CSV for example), and this would be just a normal
tuple,
>> but with custom names for the indexes.
>
>
> You apparently live in a halcyon world of data cleanliness where CSV data
is
> so well behaved.
>
> In my world, I more typically deal with stuff like
>
> data1.csv:
> --------------
> name,age,salaryK
> John,39,50
> Sally,52,37
>
> data2.csv:
> --------------
> name,salaryK,age
> Juan,47,31
> Siu,88,66
>
>
> I'm likely to define different namedtuples for dealing with this:
>
>     NameSalAge = namedtuple('NSA','name salary age')
>     NameAgeSal = namedtuple('NAS','name age salary')
>
> Then later, indeed, I might ask:
>
>     if employee1.salary == employee2.salary: ...
>
> And this would work even though I got the data from the different formats.

Then you want csv.DictReader and dictionary lookups.

ChrisA
_______________________________________________
Python-ideas mailing list
Python-ideas at python.org
https://mail.python.org/mailman/listinfo/python-ideas
Code of Conduct: http://python.org/psf/codeofconduct/
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20170730/52d68a80/attachment.html>


More information about the Python-ideas mailing list