简体   繁体   English

试图从 Python 中的字典列表中提取特定数据。 (使用罗宾股票)

[英]Trying to pull specific data from a list of dictionaries in Python. (Using Robin-Stocks)

So I'm using the robin-stocks api and I'm trying to pull specific data from a provided function.所以我正在使用 robin-stocks api 并且我试图从提供的 function 中提取特定数据。

The output when I print:我打印时的 output:

positions = r.crypto.get_crypto_positions()

is:是:

[{'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'}]

This is the documentation on their website regarding this:这是他们网站上关于此的文档:

robin_stocks.crypto.get_crypto_positions(info=None)[source]

Returns crypto positions for the account.返回账户的加密头寸。

Parameters: info (Optional[str]) – Will filter the results to get a specific value.参数: info (Optional[str]) - 将过滤结果以获取特定值。 Returns: Returns a list of dictionaries of key/value pairs for each option.返回:返回每个选项的键/值对字典列表。 If info parameter is provided, a list of strings is returned where the strings are the value of the key that matches info.如果提供了 info 参数,则返回字符串列表,其中字符串是与 info 匹配的键的值。

My goal here is to get the 'direct_cost_basis' .我的目标是获得'direct_cost_basis' How can I achieve this?我怎样才能做到这一点? I'm fairly decent at python however this is just going over my head.我在 python 上相当不错,但这只是我的头脑。

positions is a list of dicts. positions是一个字典列表。 You can access the first element in the list with您可以访问列表中的第一个元素

positions[0]

The direct_cost_basis appears to be in a dict in a list which is keyed by cost_bases , which is in the first element of positions . direct_cost_basis似乎位于列表中的字典中,该列表由cost_bases键入,它位于positions的第一个元素中。 Therefore:所以:

positions[0]["cost_bases"][0]["direct_cost_basis"]

will yield what you want.会产生你想要的。

As part of your debugging process, while printing something in Python it can be useful to also print the "type" of the object via type(positions) which would return list .作为调试过程的一部分,在 Python 中打印某些内容时,还可以通过type(positions)打印 object 的“类型”,这将返回list You can use this all the way down to determine how to access these data types.您可以一直使用它来确定如何访问这些数据类型。 You can also call the function dir(positions) on any Python object which will tell you that object's attributes and methods.您还可以在任何 Python object 上调用 function dir(positions) ,它会告诉您该对象的属性和方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM