Suppose I have this code in Python:
l = dict['link']
t = dict['title'] <<<<<<<<error here, there is no "title"
d = dict['description']
k = dict['keyword']
What if there is an error on line 2, but I want it to continue running the script and assign the other values? Can I just “ignore” the errors?
EDIT: I know how to do a simple try, except. However, Oftentimes when there is an error on #2, it will shoot to the except, and then NOT continue the rest of the code.’
EDIT: I understand that there is a “get” method. However, I’d like a GENERAL way of doing it…I won’t always be using dictionaries.
The easiest option is to use
.get():The
tvariable will then containNone(you can usedict.get('title', '')if you want an empty string, for example). Another option would be to catch theKeyErrorexception.