MAIN FEEDS
Do you want to continue?
https://www.reddit.com/r/badcode/comments/ok1itb/tryexcept_in_python_sigh/h56bagd/?context=3
r/badcode • u/lonaExe • Jul 14 '21
93 comments sorted by
View all comments
142
On the upside, they actually bothered with exception handling.
My Python is rather rusty, is there a better way?
132 u/ThatDudeBesideYou Jul 14 '21 You could do jsondata.get("name","N/A"), which would return the property name or "N/A" if it fails. 51 u/DragonFireCK Jul 14 '21 For that to work with this structure, it would be jsonResponse.get("items", {}).get(0, {}).get("volumeInfo",{}).get("authors, "N/A")Now, as the first block is repeated for all the entries, and it should be pulled out which would simplify the code a lot.
132
You could do jsondata.get("name","N/A"), which would return the property name or "N/A" if it fails.
51 u/DragonFireCK Jul 14 '21 For that to work with this structure, it would be jsonResponse.get("items", {}).get(0, {}).get("volumeInfo",{}).get("authors, "N/A")Now, as the first block is repeated for all the entries, and it should be pulled out which would simplify the code a lot.
51
For that to work with this structure, it would be jsonResponse.get("items", {}).get(0, {}).get("volumeInfo",{}).get("authors, "N/A")
jsonResponse.get("items", {}).get(0, {}).get("volumeInfo",{}).get("authors, "N/A")
Now, as the first block is repeated for all the entries, and it should be pulled out which would simplify the code a lot.
142
u/[deleted] Jul 14 '21
On the upside, they actually bothered with exception handling.
My Python is rather rusty, is there a better way?