Dave
Dave

Reputation: 165

skip error in loop - JSONDecodeError: Expecting value: line 1 column 1 (char 0)

I'm running a loop of API requests (30 requests every minute). The data comes back in JSON format which I transform into a pandas database (I identify columns and concatenate them). Sometimes I get the error below on 1 of my requests which (automatically) stops the execution of the script.

Is there a way to tell Python to just skip that error and continue the loop?

I don't mind not receiving and processing data from this one erroneous request.

If it's not that straight-forward, is there at least a way to get an audio notification (beep) so that I'm aware of it and can manually execute the script again?

Traceback (most recent call last):
  File "Skript.py", line 19, in <module>
    dfraw = pd.concat([pd.DataFrame({k: v}) for k, v in dataAPI.json().items() if k in columns], axis=1)
  File "C:\Users\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.8_qbz5n2kfra8p0\LocalCache\local-packages\Python38\site-packages\requests\models
.py", line 898, in json

  File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.8_3.8.752.0_x64__qbz5n2kfra8p0\lib\json\__init__.py", line 357, in loads
    return _default_decoder.decode(s)
  File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.8_3.8.752.0_x64__qbz5n2kfra8p0\lib\json\decoder.py", line 337, in decode
    obj, end = self.raw_decode(s, idx=_w(s, 0).end())
  File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.8_3.8.752.0_x64__qbz5n2kfra8p0\lib\json\decoder.py", line 355, in raw_decode
    raise JSONDecodeError("Expecting value", s, err.value) from None
json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)

Here is the code:

import pandas as pd
from pathlib import Path
import time
import datetime
import requests

while True:
    start = time.process_time()
    starttime = time.time()
    list_of_underlyings = ['X','Y','Z',...]

    for i in list_of_underlyings:
        url = ("https:xyzxyz?symbol=" + i + "&resolution=1&count=50&format=json")
        dataAPI = requests.get(url)
        columns = {'c', 'h', 'l', 'o', 't' , 'v'}
        dfraw = pd.concat([pd.DataFrame({k: v}) for k, v in dataAPI.json().items() if k in columns], axis=1)
        df = dfraw.reindex(columns = ['o', 'h', 'l', 'c', 'v' , 't'])

    time.sleep(60.0 - ((time.time() - starttime) % 60.0))

Upvotes: 0

Views: 1230

Answers (1)

mark pedersen
mark pedersen

Reputation: 255

you want a try except block which will try your code, and if it break it will continue to the next iteration of the loop

try:
    //your code
except:
    continue

Alternatively you can use pass instead of continue if you want it to do nothing intstead of skipping that iteration.

EDIT: if you are trying to do list comprehension, try/except blocks aren't possible, you'd need to do a regular for loop to add elements

EDIT 2: given the code, you will put the try except block inside the for loops

import pandas as pd
from pathlib import Path
import time
import datetime
import requests

while True:
    start = time.process_time()
    starttime = time.time()
    try:

        list_of_underlyings = ['X','Y','Z',...]

        for i in list_of_underlyings:
            url = ("https:xyzxyz?symbol=" + i + "&resolution=1&count=50&format=json")
            dataAPI = requests.get(url)
            columns = {'c', 'h', 'l', 'o', 't' , 'v'}
            dfraw = pd.concat([pd.DataFrame({k: v}) for k, v in dataAPI.json().items() if k in columns], axis=1)
            df = dfraw.reindex(columns = ['o', 'h', 'l', 'c', 'v' , 't'])
    except JSONDecodeError:
        continue
    finally:
        time.sleep(60.0 - ((time.time() - starttime) % 60.0))

So you will try your code, and if you run into an improperly formatted thing, it will catch it, and then finally sleep for the given time, no matter which block you go down.

Upvotes: 1

Related Questions