如何使用从EDGAR导入的字典使用他们的API



我对python很陌生,所以这可能是一个愚蠢的简单问题。我使用他们的API从EDGAR-Online导入财务报表,并将其作为字典导入。我已经阅读了我在字典上能找到的所有内容,并理解了键:值关系。但是,数据采用以下格式:

"{'result': {'totalrows': 4, 'rows': [{'rownum': 1, 'values': [{'field': 'basicepsnetincome', 'value': 0.77}, {'field': 'costofrevenue', 'value': 432592000.0}, {'field': 'Dilutedepsnetincome', 'value': 0.76}, {'field': 'ebit', 'value': 28455000.0}, {'field': 'grossprofit', 'value': 186708000.0}, {'field': 'incomebeforeextraordinaryitems', 'value': 22622000.0}, {'field': 'incomebeforetax', 'value': 31356000.0}, {'field': 'incometax', 'value':8734000.0}, {'field': 'netincome', '值':22622000.0},..."

">

字段"和"值"是我对字典的有限理解中的键,但这意味着同一键存在重复项。无论如何,我正在尝试弄清楚如何从上述数据中提取值,例如值为"28455000.0"的"ebit"。我认为它应该是 {'ebit' : 28455000.0},那么"ebit"是引用 28455000.0 的键。有关如何从上述数据集中提取数据点(例如ebit(的任何帮助,这些数据点可以引用到变量,然后随后用于计算,我们将不胜感激。

使用的请求.get

法典:

import pandas as pd 
import requests
IS = requests.get(https://datafied.api.edgar-online.com/v1/corefinancials? 
primarysymbols=MSFT&appkey={APPKEY}).json()

打印(IS(

返回上面列出的数据。

指向 EDGAR API 文档的可能有用链接 - https://developer.edgar-online.com/docs/v1#nav8

从这个网站返回的 json 有一个奇怪的结构,但只要稍加努力,你就可以得到它:

import json
from jsonpath_ng import jsonpath, parse
IS = """[your json snippet above, converted into valid json]"""
target = json.loads(IS)
fields = []
values = []
jsonpath_expr = parse('result[*].rows[*].values')
for match in jsonpath_expr.find(target):
for i in match.value:
for index, element in enumerate(i.values()):
if index % 2 == 0:
fields.append(element)
else:
values.append(element)
for f, v in zip(fields,values):
print(f,' - ', v)

输出:

basicepsnetincome  -  0.77
costofrevenue  -  432592000.0
dilutedepsnetincome  -  0.76
ebit  -  28455000.0
grossprofit  -  186708000.0
incomebeforeextraordinaryitems  -  22622000.0
incomebeforetaxes  -  31356000.0
incometaxes  -  8734000.0
netincome  -  22622000.0

相关内容

最新更新