Friday 27 October 2017

python - KeyError not caught

I am a little confused by an error I have. I have some
code that should retrieve a json file from a database and search it for a substring,
representing a code. As I understand it, my query will have a corresponding code and
then everything goes well, or my query will not have a corresponding code, and then my
code will trigger a KeyError. I have written a try/except block catching this (expected)
KeyError, but the error is not caught. What am I missing here?




Note: I just started dabbling with
multiprocessing, is this related to that?



Here
is the concerned code:



request_counter =
0
while request_counter < 3:
try:
if ' - reduced' in
metabolite:
metabolite = 'reduced ' + metabolite[:-10]


cts_output = requests.get(
'http://cts.fiehnlab.ucdavis.edu/'

'service/convert/Chemical%20Name/'
'KEGG/' + metabolite)
kegg_id =
str(json.loads(cts_output.text)[0]['result'][0])

metabolite_to_KEGG[bigg_id][kegg_id] = metabolite
request_counter =
3
except KeyError and IndexError:
if request_counter ==
2:
metabolite_no_KEGG[bigg_id].append(metabolite)


request_counter = request_counter +
1


This block is within
a couple for loops.



Traceback:




Traceback (most
recent call last): File

"/Users/dcoukos/anaconda/lib/python3.6/multiprocessing/pool.py",


line 119, in worker
result = (True, func(*args, **kwds)) File "GetKeggs.py",
line 131, in getBrendaKeggs
kegg_id =
str(json.loads(cts_output.text)[0]['result'][0]) KeyError: 'result'
"""



The above exception was the direct cause
of the following exception:



Traceback (most
recent call last): File "GetKeggs.py", line 223, in


brenda_keggs.update(bk1.get()) File
"/Users/dcoukos/anaconda/lib/python3.6/multiprocessing/pool.py",
line 644, in
get

raise self._value KeyError:
'result'


No comments:

Post a Comment

php - file_get_contents shows unexpected output while reading a file

I want to output an inline jpg image as a base64 encoded string, however when I do this : $contents = file_get_contents($filename); print &q...