简体   繁体   English

JSONDecodeError:期望值:Jupyter Notebook 中的第 1 行第 1 列(字符 0)

[英]JSONDecodeError: Expecting value: line 1 column 1 (char 0) in Jupyter Notebook

I'm following this video that uses Python, and I'm using Jupyter Notebook for the 1st project for building an equal-weight S&P 500 index fund.我正在关注这个使用 Python 的视频,并且我在第一个项目中使用 Jupyter Notebook 来构建一个等权重的标准普尔 500 指数基金。 At around 56:30 we're starting to pull data for all the stocks into a Pandas DataFrame object.在 56:30 左右,我们开始将所有股票的数据提取到 Pandas DataFrame object 中。

The loop for appending all the info is returning JSONDecodeError: Expecting value: line 1 column 1 (char 0).附加所有信息的循环返回 JSONDecodeError:预期值:第 1 行第 1 列(字符 0)。 I would also possibly like an alternate way to write this code without append, since it will be deprecated in the future.我也可能想要一种没有 append 的替代方法来编写此代码,因为它将来会被弃用。 We're using a sandbox API from IEX Cloud if that helps at all.如果有帮助的话,我们正在使用来自 IEX Cloud 的沙箱 API。

The code works when putting [:5] after 'Ticker' and returns the first 5 stock information, so I don't understand why including all the stocks gives a different response.该代码在将 [:5] 放在“Ticker”之后并返回前 5 个股票信息时起作用,所以我不明白为什么包括所有股票会给出不同的响应。

The code:代码:

stocks = pd.read_csv('sp_500_stocks.csv')
from secrets import IEX_CLOUD_API_TOKEN

my_columns = ['Ticker', 'Stock Price', 'Market Capitalization', 'Number of Shares to Buy']

final_dataframe = pd.DataFrame(columns = my_columns)
for symbol in stocks['Ticker']:
    api_url = f'https://sandbox.iexapis.com/stable/stock/{stock}/quote/?token={IEX_CLOUD_API_TOKEN}'
    data = requests.get(api_url).json()
    final_dataframe = final_dataframe.append(
        pd.Series(
        [
            symbol,
            data['latestPrice'],
            data['marketCap'],
            'N/A'
        ],
            index = my_columns
        ),
        ignore_index = True
    )

The error:错误:

    JSONDecodeError                           Traceback (most recent call last)
Input In [73], in <cell line: 2>()
      2 for stock in stocks['Ticker']:
      3     api_url = f'https://sandbox.iexapis.com/stable/stock/{stock}/quote/?token={IEX_CLOUD_API_TOKEN}'
----> 4     data = requests.get(api_url).json()
      5     final_dataframe = final_dataframe.append(
      6         pd.Series(
      7         [
   (...)
     15         ignore_index = True
     16     )

File ~\anaconda3\lib\site-packages\requests\models.py:899, in Response.json(self, **kwargs)
    897 if encoding is not None:
    898     try:
--> 899         return complexjson.loads(
    900             self.content.decode(encoding), **kwargs
    901         )
    902     except UnicodeDecodeError:
    903         # Wrong UTF codec detected; usually because it's not UTF-8
    904         # but some other 8-bit codec.  This is an RFC violation,
    905         # and the server didn't bother to tell us what codec *was*
    906         # used.
    907         pass

File ~\anaconda3\lib\json\__init__.py:346, in loads(s, cls, object_hook, parse_float, parse_int, parse_constant, object_pairs_hook, **kw)
    341     s = s.decode(detect_encoding(s), 'surrogatepass')
    343 if (cls is None and object_hook is None and
    344         parse_int is None and parse_float is None and
    345         parse_constant is None and object_pairs_hook is None and not kw):
--> 346     return _default_decoder.decode(s)
    347 if cls is None:
    348     cls = JSONDecoder

File ~\anaconda3\lib\json\decoder.py:337, in JSONDecoder.decode(self, s, _w)
    332 def decode(self, s, _w=WHITESPACE.match):
    333     """Return the Python representation of ``s`` (a ``str`` instance
    334     containing a JSON document).
    335 
    336     """
--> 337     obj, end = self.raw_decode(s, idx=_w(s, 0).end())
    338     end = _w(s, end).end()
    339     if end != len(s):

File ~\anaconda3\lib\json\decoder.py:355, in JSONDecoder.raw_decode(self, s, idx)
    353     obj, end = self.scan_once(s, idx)
    354 except StopIteration as err:
--> 355     raise JSONDecodeError("Expecting value", s, err.value) from None
    356 return obj, end

JSONDecodeError: Expecting value: line 1 column 1 (char 0)

You can achieve the same result checking the data requested is present.您可以通过检查请求的数据是否存在来获得相同的结果。

final_dataframe = pd.DataFrame(columns = my_columns)
lst = []
for stock in stocks['Ticker']:
    api_url = f'https://sandbox.iexapis.com/stable/stock/{stock}/quote/?token={IEX_CLOUD_API_TOKEN}'
    api_url = api_url.rstrip('\n')
    r = requests.get(api_url)
    if r:
        data = r.json()
        lst.append([stock, data['latestPrice'], data['marketCap'], 'N/A'])
final_dataframe = pd.DataFrame(lst, columns=my_columns)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 JSONDecodeError:期望值:python 中的第 1 行第 1 列(字符 0) - JSONDecodeError: Expecting value: line 1 column 1 (char 0) in python 熊猫:JSONDecodeError:期望值:第 1 行第 1 列(字符 0) - Pandas: JSONDecodeError: Expecting value: line 1 column 1 (char 0) JSONDecodeError:期望值:第 1 行第 1 列(字符 0) - JSONDecodeError: Expecting value: line 1 column 1 (char 0) 得到 JSONDecodeError:期望值:第 1 行第 1 列(字符 0) - Got JSONDecodeError: Expecting value: line 1 column 1 (char 0) JSONDecodeError:期望值:第 6 行第 1 列(字符 5) - JSONDecodeError: Expecting value: line 6 column 1 (char 5) JSONDecodeError:预期值:第1行第1列(字符0)错误 - JSONDecodeError: Expecting value: line 1 column 1 (char 0) error ExtensionFailed JSONDecodeError:预期值:第 1 行第 1 列(字符 0) - ExtensionFailed JSONDecodeError: Expecting value: line 1 column 1 (char 0) JSONDecodeError 期望值:第 2 行第 5 列(字符 5) - JSONDecodeError Expecting value: line 2 column 5 (char 5) JSONDecodeError:预期值:第 2 行第 1 列(字符 1) - JSONDecodeError: Expecting value: line 2 column 1 (char 1) 获取 JSONDecodeError:期望值:第 1 行第 1 列(字符 0)与 Python + Zipline + Docker + Jupyter - Getting JSONDecodeError: Expecting value: line 1 column 1 (char 0) with Python + Zipline + Docker + Jupyter
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM