How do we make an exception in the cycle, continue the violin?
-
Tell me how to do the exclusion cycle. Example:
user_json = json.loads(req.text)
for acc in user_json['result']['per']:
try:
if user_json['result']['per'] is not None:
account.append(str(acc['name']))
else:
account.append("mail.ru")
except:
account.append("mail.ru")
Such decisions do not work because if there is no result in requests in json, the crypt falls
for acc in user_json['result']['per']:
KeyError: 'result'
And I need to have if it wasn't even important, he added a default value in this case mail.ru
p.s. sometimes iterations don't add value, the things that result, they're gone, so at this point the violin falls, and I need to keep it going.
-
One line:
account = [acc.get('name', "mail.ru") for acc in (user_json['result']['per'] if 'result' in user_json and 'per' in user_json['result'] else [{}])]