因此,我正在使用robin-stocks,并尝试从提供的函数中提取特定数据。
打印时的输出:
positions = r.crypto.get_crypto_positions()
是:
[{'account_id': 'removed', 'cost_bases': [{'currency_id': 'removed', 'direct_cost_basis': '0.110000000000000000', 'direct_quantity': '0.000011050000000000', 'id': 'removed', 'intraday_quantity': '0.000000000000000000', 'intraday_cost_basis': '0.000000000000000000', 'marked_cost_basis': '0.000000000000000000', 'marked_quantity': '0.000000000000000000'}], 'created_at': '2020-05-07T20:44:23.510080-04:00', 'currency': {'brand_color': 'EA963D', 'code': 'BTC', 'id': 'removed', 'increment': '0.000000010000000000', 'name': 'Bitcoin', 'type': 'cryptocurrency'}, 'id': 'removed', 'quantity': '0.000011050000000000', 'quantity_available': '0.000011050000000000', 'quantity_held_for_buy': '0.000000000000000000', 'quantity_held_for_sell': '0.000000000000000000', 'updated_at': '2020-05-07T23:40:34.381524-04:00'}]这是他们网站上关于这一点的文档:
robin_stocks.crypto.get_crypto_positions(info=None)[source]
返回帐户的加密位置。
参数: info (Optionalstr) -将过滤结果以获得特定值。返回:返回每个选项的键/值对的字典列表。如果提供了info参数,则返回字符串列表,其中字符串是与info匹配的键的值。
我在这里的目标是获得'direct_cost_basis'。我如何才能做到这一点?我对python相当在行,但是这让我摸不着头脑。
发布于 2020-05-08 13:04:37
positions是一个字典列表。您可以使用以下命令访问列表中的第一个元素
positions[0]direct_cost_basis似乎位于由cost_bases键入的列表中的字典中,该列表位于positions的第一个元素中。因此:
positions[0]["cost_bases"][0]["direct_cost_basis"]会得到你想要的。
作为调试过程的一部分,在使用Python语言打印内容的同时,还可以通过返回list的type(positions)打印对象的“类型”。您可以一直使用它来确定如何访问这些数据类型。您还可以在任何Python对象上调用函数dir(positions),该函数将告诉您该对象的属性和方法。
https://stackoverflow.com/questions/61672019
复制相似问题