我正在尝试从polygon.io API中提取财务数据以对其进行组织,然后将其注入到Azure数据库中。为了实现这一点,我一直在使用“ petl” python程序包,但是由于示例中的源参数无效,我遇到了一些问题,我认为这是由我检索和编写JSON结果的方式引起的。>
import petl as etl
import websocket
import json
import requests
r =requests.get("https://api.polygon.io/v1/historic/forex/AUD/USD/2018-2-2?limit=1&apiKey=*********")
#gets data includes plenty of responses that arnt text
data = json.loads(r.text)
table1 = etl.fromjson(data, header=['day','map','msLatency','pair','status','ticks','type'])
print(table1)
我期望一个表的列标题为下面列出其值的列标题,但收到此错误消息:"AssertionError: invalid source argument, expected None or a string or an object implementing open()"
从API打印数据时,它看起来像这样:
{'day': '2018-2-2', 'map': {'a': 'ask', 'b': 'bid', 't': 'timestamp'}, 'msLatency': 1, 'pair': 'AUD/USD', 'status': 'success', 'ticks': [{'b': 0.80392, 'a': 0.80392, 'x': 0, 't': 1517529600225}], 'type': 'forex'}
我曾尝试过转换数据或使用api调用行作为数据参数,但收效甚微
答案 0 :(得分:0)
petl.fromjson
调用需要一个字符串(本地文件的路径)或无(stdin)。您已从URL中提取了JSON(文本)并将其转换为Python字典。因此,您应该考虑词典,而不是源的JSON格式。我可以看到几种可能的解决方案:
json.load
,只需将响应写入临时文件,然后在文件上调用petl.fromjson。json.load
,然后再调用petl.fromdicts。