JSONDecodeError skip

Viewed 120

I am trying to get a new column in a dataframe with the function passed in it and this is a working code. (The function is an API scrape and idk how to 1) edit it or 2) trouble shoot it)

df = pd.DataFrame(data = myarray)
df.columns=["A"]
df['B']= 'US'

array_thing=[]

for i in range(len(df)):
    array_thing.append(mycoolfunction(df.iloc[i, 0], df.iloc[i, 1]))
df['new']=array_thing

df

Now I need to skip the error that is raised in some lines saying: JSONDecodeError: Expecting value: line 1 column 1 (char 0)

Ive already tried looking at other stackoverflows and every possible 'try''continue' codes, im so confused about what to do.

I have tried the following:

try:
    for i in range(len(df)):
        array_thing.append(mycoolfunction(df.iloc[i, 0], df.iloc[i, 1]))
        df['new']=array_thing
    except JSONDecodeError as e:
        continue

for i in range(len(df)):
    try:
        array_thing.append(mycoolfunction(df.iloc[i, 0], df.iloc[i, 1]))
        df['new']=array_thing
    except <JSONDecodeError: Expecting value: line 1 column 1 (char 0)> as e:
        print('error!')
df

none of it works.. I just need it to skip over the error and either not produce anything in that row or just say error and continue.

something like this in a dataframe: | a| b| jsoncode| |:---- |:------:| -----:| | A123| US | Error|

and then just continue showing the json for all those that work.

thank you!!

1 Answers

if you want to treat diferent errors, try just:

import traceback

for i in range(len(df)):
    try:
        array_thing.append(mycoolfunction(df.iloc[i, 0], df.iloc[i, 1]))
        df['new']=array_thing
    except Exception as e:
        print(traceback.format_exc())
        print("your traceback", e)

Or if you just want to ignore any errors, just:

...   
except:
        pass
Related