使用for循环格式化python dict



我正在尝试编写一个通过assets(BTC,EOS(和available_balance(0.00087168,0(的for循环。assets工作,但我无法到达available_balance。我怎么能用json实现它呢。

import json 
pairs= '''
({
'ext_code': '',
'rate_limit_status': 118,
'result': {
'BTC': {'available_balance': 0.00087168, 'cum_realised_pnl': 7.288e-05, 'equity': 0.00087168, 'given_cash': 0},
'EOS': {'available_balance': 0, 'cum_realised_pnl': 0, 'equity': 0, 'given_cash': 0}
},
'ret_code': 0,
'ret_msg': 'OK',
'time_now': '1619987733.732306'}
<bravado.requests_client.RequestsResponseAdapter object at 0x000002D6F7FEB808>)
'''
data = json.loads(pairs)
Wallet_balance = data[0]['result']
for assets in balance:
print("Asset: ", assets, " Balance: ", assets['available_balance'])

预期输出:

Asset: BTC Balance: 0.00087168
Asset: EOS Balance: 0

pprint输出:

({'ext_code': '',
'ext_info': '',
'rate_limit': 120,
'rate_limit_reset_ms': 1620081183677,
'rate_limit_status': 117,
'result': {'BTC': {'available_balance': 0.00087168,
'cum_realised_pnl': 7.288e-05,
'equity': 0.00087168,
'given_cash': 0,
'occ_closing_fee': 0,
'occ_funding_fee': 0,
'order_margin': 0,
'position_margin': 0,
'realised_pnl': 0,
'service_cash': 0,
'unrealised_pnl': 0,
'used_margin': 0,
'wallet_balance': 0.00087168},
'EOS': {'available_balance': 0,
'cum_realised_pnl': 0,
'equity': 0,
'given_cash': 0,
'occ_closing_fee': 0,
'occ_funding_fee': 0,
'order_margin': 0,
'position_margin': 0,
'realised_pnl': 0,
'service_cash': 0,
'unrealised_pnl': 0,
'used_margin': 0,
'wallet_balance': 0},
'ETH': {'available_balance': 0.03362706,
'cum_realised_pnl': -7.41e-06,
'equity': 0.03362706,
'given_cash': 0,
'occ_closing_fee': 0,
'occ_funding_fee': 0,
'order_margin': 0,
'position_margin': 0,
'realised_pnl': 0,
'service_cash': 0,
'unrealised_pnl': 0,
'used_margin': 0,
'wallet_balance': 0.03362706},
'USDT': {'available_balance': 0,
'cum_realised_pnl': 0,
'equity': 0,
'given_cash': 0,
'occ_closing_fee': 0,
'occ_funding_fee': 0,
'order_margin': 0,
'position_margin': 0,
'realised_pnl': 0,
'service_cash': 0,
'unrealised_pnl': 0,
'used_margin': 0,
'wallet_balance': 0},
'XRP': {'available_balance': 0,
'cum_realised_pnl': 0,
'equity': 0,
'given_cash': 0,
'occ_closing_fee': 0,
'occ_funding_fee': 0,
'order_margin': 0,
'position_margin': 0,
'realised_pnl': 0,
'service_cash': 0,
'unrealised_pnl': 0,
'used_margin': 0,
'wallet_balance': 0}},
'ret_code': 0,
'ret_msg': 'OK',
'time_now': '1620081183.700541'},
<bravado.requests_client.RequestsResponseAdapter object at 0x0000016DC011F888>)

请注意,您的json无效。除了使用单引号之外,它只是缺少了一些部分。

假设你有以下任何一种,那么这应该会让你开始:

import json
import ast
## -----------------------------
## I have valid json
## -----------------------------
pairs_double_quoted = '''
[{
"ext_code": "",
"rate_limit_status": 118,
"result": {
"BTC": {"available_balance": 0.00087168, "cum_realised_pnl": 7.288e-05, "equity": 0.00087168, "given_cash": 0},
"EOS": {"available_balance": 0, "cum_realised_pnl": 0, "equity": 0, "given_cash": 0}
},
"ret_code": 0,
"ret_msg": "OK",
"time_now": "1619987733.732306"
}]
'''
## -----------------------------
## -----------------------------
## I have something else that still looks hopeful
## -----------------------------
pairs_single_quoted = '''
({
'ext_code': '',
'rate_limit_status': 118,
'result': {
'BTC': {'available_balance': 0.00087168, 'cum_realised_pnl': 7.288e-05, 'equity': 0.00087168, 'given_cash': 0},
'EOS': {'available_balance': 0, 'cum_realised_pnl': 0, 'equity': 0, 'given_cash': 0}
},
'ret_code': 0,
'ret_msg': 'OK',
'time_now': '1619987733.732306'
})
'''
## -----------------------------
## -----------------------------
## one or the other depending on if you have proper json (double) quotes
## -----------------------------
data = json.loads(pairs_double_quoted)
#data = [ast.literal_eval(pairs_single_quoted)]
## -----------------------------
## -----------------------------
## data is a list so we likely want to go though each item on the list
## if you only want the first item then use
## data_item = data[0]
## -----------------------------
for data_item in data:
## -----------------------------
## for each key value pair in the result dict print something
## -----------------------------
for asset_key, asset_value in data_item["result"].items():
print("Asset: {} Balance: {}".format(asset_key, asset_value["available_balance"]))
## -----------------------------
## -----------------------------

根据您的更新,您所拥有的数据正是:

pairs_single_quoted = '''
({
'ext_code': '',
'rate_limit_status': 118,
'result': {
'BTC': {'available_balance': 0.00087168, 'cum_realised_pnl': 7.288e-05, 'equity': 0.00087168, 'given_cash': 0},
'EOS': {'available_balance': 0, 'cum_realised_pnl': 0, 'equity': 0, 'given_cash': 0}
},
'ret_code': 0,
'ret_msg': 'OK',
'time_now': '1619987733.732306'
}<bravado.requests_client.RequestsResponseAdapter object at 0x000002D6F7FEB808>)
'''

出于某种原因,那么你可能想走ast路线:

import re
import ast
## -----------------------------
## I have something else that still looks hopeful
## -----------------------------
pairs_single_quoted = '''
({
'ext_code': '',
'ext_info': '',
'rate_limit': 120,
'rate_limit_reset_ms': 1620081183677,
'rate_limit_status': 117,
'result': {
'BTC': {'available_balance': 0.00087168, 'cum_realised_pnl': 7.288e-05, 'equity': 0.00087168, 'given_cash': 0, 'occ_closing_fee': 0, 'occ_funding_fee': 0, 'order_margin': 0, 'position_margin': 0, 'realised_pnl': 0, 'service_cash': 0, 'unrealised_pnl': 0, 'used_margin': 0, 'wallet_balance': 0.00087168},
'EOS': {'available_balance': 0, 'cum_realised_pnl': 0, 'equity': 0, 'given_cash': 0, 'occ_closing_fee': 0, 'occ_funding_fee': 0, 'order_margin': 0, 'position_margin': 0, 'realised_pnl': 0, 'service_cash': 0, 'unrealised_pnl': 0, 'used_margin': 0, 'wallet_balance': 0},
'ETH': {'available_balance': 0.03362706, 'cum_realised_pnl': -7.41e-06, 'equity': 0.03362706, 'given_cash': 0, 'occ_closing_fee': 0, 'occ_funding_fee': 0, 'order_margin': 0, 'position_margin': 0, 'realised_pnl': 0, 'service_cash': 0, 'unrealised_pnl': 0, 'used_margin': 0, 'wallet_balance': 0.03362706}
},
'ret_code': 0,
'ret_msg': 'OK',
'time_now': '1620081183.700541'},
<bravado.requests_client.RequestsResponseAdapter object at 0x0000016DC011F888>)
'''
## -----------------------------
## -----------------------------
## a regex wizard would do this via re.sub()
## -----------------------------
pattern = re.compile(r"^(.*),.*<bravado.*$", re.DOTALL)
text_in = re.findall(pattern, pairs_single_quoted)[0] + ")"
data = [ast.literal_eval(text_in)]
## -----------------------------
## -----------------------------
## data is a list so we likely want to go though each item on the list
## if you only want the first item then use
## data_item = data[0]
## -----------------------------
for data_item in data:
## -----------------------------
## for each key value pair in the result dict print something
## -----------------------------
for asset_key, asset_value in data_item["result"].items():
print("Asset: {} Balance: {}".format(asset_key, asset_value["available_balance"]))
## -----------------------------
## -----------------------------

这将导致:

Asset: BTC Balance: 0.00087168
Asset: EOS Balance: 0
Asset: ETH Balance: 0.03362706

最新更新