RknRobin
RknRobin

Reputation: 401

How can I add multiple keys to a python Dictionary?

I have a web scraper that grabs info and saves it to a database. I use the following code to save data.

try: 
    base['vevo']['url']
except:
    base['vevo']['url'] = "NotGiven"
try: 
    base['vevo']['viewsLastWeek']['data']['time']
except:
    base['vevo']['viewsLastWeek']['data']['time'] = '2199-01-01'

Now normally this works, however ocassionally the data stream doesn't return any info at all for base['vevo']. This breaks the above dict add and says that KeyError 'vevo'.

I've been trolling through other stackoverflow questions, but I haven't been able to find anything that references adding multiple keys at once like I'm trying to do. I've tried to use base.append('key'), tried base.get() but couldn't find a reference on how to use it for multiple keys deep. Any ideas on how to get around it?

Upvotes: 0

Views: 952

Answers (2)

RknRobin
RknRobin

Reputation: 401

So I found a solution, but it involved a change in logic instead of what I had originally tried to do.

Since I was only using the dictionary value to save to my database, I could use a place holder variable as an in-between for the functions. See below for the working code..

try: 
    v_url = base['vevo']['url']
except:
    v_url = "NotGiven"

Adding values to the existing dictionary proved to be too complicated, and this solution involves no extra packages.

Upvotes: 0

You can use defaultdict.

import collections
def new_level():
    return collections.defaultdict(new_level)
base=new_level()

This would allow you to add an arbitrary number of levels to your nested dicts:

 >>> base["foo"]["bar"]["foobar"]=42
 {'foo': {'bar': {'foobar': 42}}}

Upvotes: 1

Related Questions