Python Forum
Accessing nested keys and values in Dictreader
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Accessing nested keys and values in Dictreader
#1
Hello,
I'm using Dictreader to store a CSV with column headers in a container. I didn't have any issues bringing the CSV into the
container, but I can't seem to grasp how to access the nested keys and values. From what I can tell, the 1st column is my primary key, but then subsequent column headers are also keys with values. I'm confused on how to retrieve a value from one of the sub-keys. For example, my primary key which I specified as a fieldname is called "Cars". I need to first find "Cars", then a second associated column (also a key)called "Model"...and then the associated value of "Model". Hopefully, that makes sense: Cars > Model > Value of Model.
Reply
#2
Something along those lines?

>>> [row[“Model”] for row in my_dict if row[“Cars”] == “Tesla”]
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply
#3
Thanks for the reply. I believe that's what I'm looking for. I'll give it shot. Much appreciated.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  create empty sets for keys in a dictionary and add values to the set naughtysensei 1 2,481 Nov-03-2020, 08:32 AM
Last Post: DeaD_EyE

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020