1

我正在从 api 端点获取财务信息,当我收到 200 响应时

r = requests.get(url)
data = r.json()

它将返回None所有null值。如何将所有null/None值转换为0?由于它是财务数据,因此 JSON 通常非常庞大(300k-400k 行,有些嵌套很深nulls),所以我不能try/except对每个TypeError.

json 响应的摘录如下所示:

{'0': 
'Highlights': {'QuarterlyRevenueGrowthYOY': 0.671, 'GrossProfitTTM': 3750684, 'DilutedEpsTTM': 0.2, 'QuarterlyEarningsGrowthYOY': 0.95
5}, 'Valuation': {'TrailingPE': 60.75, 'ForwardPE': 0, 'PriceSalesTTM': 2.0817, 'PriceBookMRQ': 4.207, 'EnterpriseValueRevenue': 1.
806, 'EnterpriseValueEbitda': 0.0952}, 'Technicals': {'Beta': None, '52WeekHigh': 12.35, '52WeekLow': 7.84, '50DayMA': 11.0197, '20
0DayMA': 10.2209, 'SharesShort': 0, 'SharesShortPriorMonth': 0, 'ShortRatio': 0, 'ShortPercent': 0}, 'SplitsDividends': {'ForwardAn
nualDividendRate': 0.18, 'ForwardAnnualDividendYield': 0.0151, 'PayoutRatio': 0.9, 'DividendDate': '0000-00-00', 'ExDividendDate':
'2020-06-11', 'LastSplitFactor': '', 'LastSplitDate': '0000-00-00'}, 'Earnings': {'Last_0': {'date': '2020-06-30', 'epsActual': 0.1
9, 'epsEstimate': None, 'epsDifference': None, 'surprisePercent': None}, 'Last_1': {'date': '2019-12-31', 'epsActual': 1.86, 'epsEs
timate': None, 'epsDifference': None, 'surprisePercent': None}, 'Last_2': {'date': '2019-06-30', 'epsActual': -0.82, 'epsEstimate':
 None, 'epsDifference': None, 'surprisePercent': None}, 'Last_3': {'date': '0000-00-00', 'epsActual': 0, 'epsEstimate': 0, 'epsDiff
erence': 0, 'surprisePercent': 0}}, 'Financials': {'Balance_Sheet': {'currency_symbol': 'EUR', 'quarterly_last_0': {'date': '2020-0
6-30', 'filing_date': None, 'totalAssets': '12810000.00', 'intangibleAssets': '281000.00', 'otherCurrentAssets': '60000.00', 'total
Liab': '4225000.00', 'totalStockholderEquity': '8585000.00', 'deferredLongTermLiab': '74000.00', 'otherCurrentLiab': '1274000.00',
'commonStock': '80000.00', 'retainedEarnings': '311000.00', 'otherLiab': '200000.00', 'goodWill': '3381000.00', 'otherAssets': '730
00.00', 'cash': '4983000.00', 'totalCurrentLiabilities': '4025000.00', 'shortLongTermDebt': None,
... 
} 

是的,你明白了……None到处都是。对此有任何快速修复吗?

4

2 回答 2

1
def recursive_replace(obj, findVal, replaceVal):
  for k, v in obj.items():
    if v == findVal:
      obj[k] = replaceVal
    elif isinstance(v, dict):
      obj[k] = recursive_replace(obj[k], findVal, replaceVal)
  return obj

result = recursive_replace(json.loads(yourdata), None, 0)
于 2021-02-07T18:05:22.500 回答
0

Found a way to do it, @Charles Duffy, thanks for the inspiration - borrowed some but couldn't get it quite to work. The final code looks like this if anyone would need it in the future

from collections.abc import Mapping, Iterable


def replace_none_values(noneVal, replaceVal='0.00'): # not sure if this is bad practice 
    if noneVal is None:
        return replaceVal
    if isinstance(noneVal, Mapping):
        return {k: replace_none_values(v, replaceVal) for k, v in noneVal.items()}
    elif not isinstance(noneVal, str) and isinstance(noneVal, Iterable):
        return [replace_none_values(v, replaceVal) for v in noneVal]
    return noneVal
于 2021-02-08T07:43:35.137 回答