Quantcast
Channel: Active questions tagged python - Stack Overflow
Viewing all articles
Browse latest Browse all 23160

python try except misuse - what should i use instead? [duplicate]

$
0
0

I'm working with an external API and trying to automatically feed data from it into my own application.Let's say that the API call i'm making always has the fields 'length' and 'girth' and sometimes has the field 'colour'.

The way I have currently set it up to extract the data from the API call is like this:

python

resp = request.get(some_api_call)length = resp.json()['length']girth = resp.json()['girth']try:   colour = resp.json()['colour']except:   colour = 'unknown'# do something with data all three variables.

My question is how should i do this without using try and except? I am sure I am not using it how it was intended and I want to ensure good quality code.


Viewing all articles
Browse latest Browse all 23160

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>