python - Save KeyErrors and IndexErrors to a list/dict -
i'm trying extract data api, , expect receive keyerror , indexerror, use try/except function catch them. first create list of items i'll loop through extract information api responses. create dataframe stores information items had no errors.
l= ["a","b","c","d"] def extract_info_from_one_response(response, idx): try: response = response.json() d = { ### codes ## } ## error management except keyerror,e: print idx, l[idx], str(e) return {} except indexerror,e: print idx, l[idx], str(e) return {} dat = pd.dataframe([extract_info_from_one_response(response, idx) idx, response in enumerate(responses)], index=l)
when errors occur, python prints out [1]the index of problematic item, [2] name of item , [3]the details on error took place. how save/capture these 3 outputs, save them objects or create dataframe these 3 pieces of information?
are asking how trace error?
if so,the traceback module this:
in [1]: import traceback in [2]: try: ...: = [] ...: a[2] = 1 ...: except exception: ...: y = traceback.format_exc() ...: in [4]: print y traceback (most recent call last): file "<ipython-input-2-ac34fe2721d3>", line 3, in <module> a[2] = 1 indexerror: list assignment index out of range
Comments
Post a Comment