I would like to organize data currently in a spreadsheet (or CSV) using Python dictionaries. Specifically, I want to be able to extract a specific value if given the person's name.
My idea is to have a dictionary for each name in my list. For example,
| name | email | phone | address |
|-------|----------------|-------|---------|
| Alex | alex@site.com | 111 | APT 1 |
| Ben | ben@site.com | 222 | APT 2 |
| Chris | chris@site.com | 333 | APT 3 |
would be coded as
Alex = {'email':'alex@site.com', 'phone':'111', 'address':'APT 1'}
Ben = {'email':'ben@site.com', 'phone':'222', 'address':'APT 2'}
Chris = {'email':'chris@site.com', 'phone':'333', 'address':'APT 3'}
where of course I could use a CSV reader and variables in place of the dictionary values. Doing it this way, if I want Alex's email, I could use Alex['email']
.
This works great except I'd like to automate this for a long list of names rather than defining each dictionary individually. As far as I know, variables can't be used to name dictionaries. This seems like a pretty common situation and I'd like to know the best way to go about this, dictionaries or otherwise.