简体   繁体   English

如何从字典中提取数据?

[英]How to extract data from a dictionary?

I am trying to get the asset/free/locked fields along with the corresponding data to populate into columns.我正在尝试获取资产/空闲/锁定字段以及相应的数据以填充到列中。 Currently, I can only get the balances column where these fields fall.目前,我只能获取这些字段所在的余额列。

Here is the data format.这是数据格式。 I don't need anything before 'balances'.在“平衡”之前我不需要任何东西。 Thinking if I could remove this part of the string maybe then the columns would be created?想如果我可以删除这部分字符串,那么列会被创建吗? Or if there is a another way to do this?或者如果有另一种方法可以做到这一点?

'{'makerCommission': 10, 'takerCommission': 10, 'buyerCommission': 0, 'sellerCommission': 0, 'canTrade': True, 'canWithdraw': True, 'canDeposit': True, 'updateTime': 1595872633345, 'accountType': 'MARGIN', 'balances': [{'asset': 'BTC', 'free': '0.00000000', 'locked': '0.00000000'}, {'asset': 'LTC', 'free': '0.00000000', 'locked': '0.00000000'}, {'asset': 'ETH', 'free': '0.00000000', 'locked': '0.00000000'}...'

The code so far to get the balances is:到目前为止,获取余额的代码是:

account = client.get_account()

assets = pd.DataFrame(account, columns = ['balances'])

Any help appreciated.任何帮助表示赞赏。 Got me stumped.把我难住了。

from ast import literal_eval
import pandas as pd

# if account is a string
assets = pd.json_normalize(literal_eval(account), 'balances')

# if account is a dict
assets = pd.json_normalize(account, 'balances')

# display(assets)
  asset        free      locked
0   BTC  0.00000000  0.00000000
1   LTC  0.00000000  0.00000000
2   ETH  0.00000000  0.00000000

Sample Data as a str样本数据作为str

data = "{'makerCommission': 10, 'takerCommission': 10, 'buyerCommission': 0, 'sellerCommission': 0, 'canTrade': True, 'canWithdraw': True, 'canDeposit': True, 'updateTime': 1595872633345, 'accountType': 'MARGIN', 'balances': [{'asset': 'BTC', 'free': '0.00000000', 'locked': '0.00000000'}, {'asset': 'LTC', 'free': '0.00000000', 'locked': '0.00000000'}, {'asset': 'ETH', 'free': '0.00000000', 'locked': '0.00000000'}]}"

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

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