在python中解析带有浮点数作为字符串的json字符串
我正在编写一个类对象,它使用python请求从URL读取json。json字符串很奇怪,因为浮点数、日期和整数都是字符串在python中解析带有浮点数作为字符串的json字符串,python,json,python-requests,Python,Json,Python Requests,我正在编写一个类对象,它使用python请求从URL读取json。json字符串很奇怪,因为浮点数、日期和整数都是字符串 { "financialStatementList" : [ { "symbol" : "HUBS", "financials" : [ { "date" : "2018-12-31", "Revenue" : "512980000.0", "Revenue Growth" : "0.3657", "Cost of Revenue" : "100357000.
{
"financialStatementList" : [ {
"symbol" : "HUBS",
"financials" : [ {
"date" : "2018-12-31",
"Revenue" : "512980000.0",
"Revenue Growth" : "0.3657",
"Cost of Revenue" : "100357000.0",
"Gross Profit" : "412623000.0",
"R&D Expenses" : "117603000.0",
"SG&A Expense" : "343278000.0",
"Operating Expenses" : "460881000.0",
"Operating Income" : "-48258000.0",
"Interest Expense" : "21386000.0",
"Earnings before Tax" : "-61960000.0",
"Income Tax Expense" : "1868000.0",
"Net Income - Non-Controlling int" : "0.0",
"Net Income - Discontinued ops" : "0.0",
"Net Income" : "-63828000.0",
"Preferred Dividends" : "0.0",
"Net Income Com" : "-63828000.0",
"EPS" : "-1.66",
"EPS Diluted" : "-1.66",
"Weighted Average Shs Out" : "39232269.0",
"Weighted Average Shs Out (Dil)" : "38529000.0",
"Dividend per Share" : "0.0",
"Gross Margin" : "0.8044",
"EBITDA Margin" : "-0.033",
"EBIT Margin" : "-0.0791",
"Profit Margin" : "-0.124",
"Free Cash Flow margin" : "0.1002",
"EBITDA" : "-17146000.0",
"EBIT" : "-40574000.0",
"Consolidated Income" : "-63828000.0",
"Earnings Before Tax Margin" : "-0.1208",
"Net Profit Margin" : "-0.1244"
}
api端点处的json示例如下:
我的问题是,当我解码时,我得到的是对象/字符串,而不是浮点数或整数
我试过:
r = requests.get(url, params)
jd = json.loads(r.text)
以及:
r = requests.get(url, params)
jd - r.json()
还有使用KWARG的变体,如parse_float=float或parse_float=Decimal
我的最终目标是将其转换为float、int和dates格式。我最终需要为我的json解码器编写一个自定义对象挂钩 我还决定添加一个camellizer来缩短键
import requests
import re
import json
from datetime import datetime
quarterdateformat = '%Y-%m-%d'
def camelize(string):
return "".join(string.split(" "))
def convert_types(d):
for k, v in d.items():
#print(k, type(v))
new_v = v
if type(v) is str:
#match for float
if re.match('[-+]?[0-9]*\.[0-9]+', v):
new_v = float(v)
#match for date
if re.match('([12]\d{3}-(0[1-9]|1[0-2])-(0[1-9]|[12]\d|3[01]))', v):
new_v = datetime.strptime(v, quarterdateformat).date()
d[k] = new_v
d = {camelize(k): v for k, v in d.items()}
return d
url = "https://financialmodelingprep.com/api/v3/financials/income-statement/CRM,HUBS"
params = {'datatyupe' : 'json'}
r = requests.get(url, params)
jd= json.loads(r.text, object_hook=convert_types)
convert_types是一个对象钩子函数,它使用正则表达式查找浮点数和日期并进行转换。camelizer在对象钩子的末尾用于将所有键转换为CamelCase。响应实际上是在给您字符串,因此
请求
无法在那里发挥任何魔力并将它们转换为数字。你必须自己去做,我认为这样做的方法是在json.loads中使用object\u pairs\u hooks参数,并将它指向一个自定义函数?很难找到该函数所需外观的示例。如果我是你,我就不会把时间浪费在re
东西上。首先尝试int
——将其包装在try/catch
块中,如果有效的话——您有int。下一步,float
,再次在try/catch
块中。最后一个--日期转换。是的,我很懒。我的字符串中没有任何exp符号。