【问题标题】:Error when reading yahoofinancials JSON: Unexpected character found when decoding 'NaN'读取 yahoofinancials JSON 时出错:解码“NaN”时发现意外字符
【发布时间】:2021-03-09 14:18:38
【问题描述】:

我正在尝试读取从 python 包“yahoofinancials”获取的 json(它从 Yahoo Finance 提取数据):

import numpy as np
import pandas as pd
from yahoofinancials import YahooFinancials

yahoo_financials = YahooFinancials(ticker)
cash_statements = yahoo_financials.get_financial_stmts('annual', 'income')
cash_statements
pd.read_json(str(cash_statements).replace("'", '"'), orient='records')

但是我得到了错误: 解码“NaN”时发现意外字符

【问题讨论】:

    标签: python json yahoo-finance


    【解决方案1】:

    检查文件是否可用或文件名是否正确,因为我在读取不在该文件夹中且位于其他位置的.json 文件时遇到了同样的错误。

    【讨论】:

      【解决方案2】:

      问题是这个命令:str(cash_statements).replace("'", '"')

      您尝试通过将单引号替换为双引号将 "convert"python 字典 转换为 json 字符串,但此操作不正确工作。

      使用 json.dump(cash_statements) 函数将您的字典对象转换为 json 字符串。

      更新代码:

      import numpy as np
      import pandas as pd
      from yahoofinancials import YahooFinancials
      
      # ADJUSTMENT 1 - import json
      import json
      
      
      # just some sample data for testing
      ticker = ['AAPL', 'MSFT', 'INTC']
      
      yahoo_financials = YahooFinancials(ticker)
      cash_statements = yahoo_financials.get_financial_stmts('annual', 'income')
      
      # ADJUSTMENT 2 - dict to json
      cash_statements_json = json.dumps(cash_statements)
      pd.read_json(cash_statements_json, orient='records')
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2022-01-01
        • 2020-07-22
        • 2016-11-14
        • 2014-10-14
        • 1970-01-01
        • 2023-02-08
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多