簡體   English   中英

如何從 python 中的字典中提取某些信息?

[英]How to extract certain information from a dict in python?

我正在嘗試使用 python 編寫交易機器人算法。

代碼運行時:

specificID = auth_client.get_balances(assets = 'ZAR' )
print(specificID)

output 是:

{'balance': [{'account_id': '7008143899323135806', 'asset': 'ZAR', 'balance': '200.692439', 'reserved': '0.00', 'unconfirmed': '0.00'}]}

我的問題是:

如何將 '200.692439 信息提取為浮點值?

owned = float(balance)其中余額為“200.692439”

  • '200.692439'的值不是固定值,會發生變化。 *

到目前為止我所做的:我將 specificID 信息編碼到一個文本文檔中,然后選擇 readline 並將 200.692439 提取到一個元組中。

f = open("demo.txt", "w")
f.write(str(specificID))
f.close()

with open('demo.txt', 'r') as handle:
    first_line = handle.readline()
    tuple_balance = first_line[79],first_line[80],first_line[81],first_line[82],first_line[83],first_line[84],first_line[85]

我正在尋找一種將元組轉換為浮點數的方法,或者尋找一種更簡單的方法從字典中提取“200.692439”,以便它可以用作浮點數

您可以使用

float(d['balance'][0]['balance'])

[0]是由於您存儲在balance下的值是一個list


d['balance']
#[{'account_id': '7008143899323135806', 'asset': 'ZAR', 'balance': '200.692439', 'reserved': '0.00', 'unconfirmed': '0.00'}]


d['balance'][0]
#{'account_id': '7008143899323135806', 'asset': 'ZAR', 'balance': '200.692439', 'reserved': '0.00', 'unconfirmed': '0.00'}

float(d['balance'][0]['balance'])
#200.692439

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM