【问题标题】:Uisng Json to format and indent in website requests Python在网站请求中使用 Json 格式化和缩进 Python
【发布时间】:2021-03-25 15:30:13
【问题描述】:

我正在尝试对balance 的格式进行缩进和分类,以便于阅读。我想像Expected output 一样打印RequestResponsebalance 变量的类型为 tuple。我怎么可能做这种事?

import bybit
import json
balance = client.Wallet.Wallet_getBalance(coin="BTC").result()
print(balance)

输出:

({'ret_code': 0, 'ret_msg': 'OK', 'ext_code': '', 'ext_info': '', 'result': {'BTC': {'equity': 0.00208347, 'available_balance': 0.00208347, 'used_margin': 0, 'order_margin': 0, 'position_margin': 0, 'occ_closing_fee': 0, 'occ_funding_fee': 0, 'wallet_balance': 0.00208347, 'realised_pnl': 0, 'unrealised_pnl': 0, 'cum_realised_pnl': 8.347e-05, 'given_cash': 0, 'service_cash': 0}}, 'time_now': '1616685310.655072', 'rate_limit_status': 118, 'rate_limit_reset_ms': 1616685310652, 'rate_limit': 120}, <bravado.requests_client.RequestsResponseAdapter object at 0x000001F5E92EB048>)

预期输出:

{
  "cross_seq": 11518,
  "data": [
    {
      "price": "2999.00",
      "side": "Buy",
      "size": 9,
      "symbol": "BTCUSD"
    },
    {
      "price": "3001.00",
      "side": "Sell",
      "size": 10,
      "symbol": "BTCUSD"
    }
  ],
  "timestamp_e6": 1555647164875373,
  "topic": "orderBookL2_25.BTCUSD",
  "type": "snapshot"
}

【问题讨论】:

    标签: python json dictionary format tuples


    【解决方案1】:

    我认为您提供了错误的预期输出,因为您的输出和预期输出之间的字段不匹配,但一般来说,如果您想要更好地显示字典,您可以使用 json 包:

    response = {'ret_code': 0, 'ret_msg': 'OK', 'ext_code': '', 'ext_info': '', 'result': {'BTC': {'equity': 0.00208347, 'available_balance': 0.00208347, 'used_margin': 0, 'order_margin': 0, 'position_margin': 0, 'occ_closing_fee': 0, 'occ_funding_fee': 0, 'wallet_balance': 0.00208347, 'realised_pnl': 0, 'unrealised_pnl': 0, 'cum_realised_pnl': 8.347e-05, 'given_cash': 0, 'service_cash': 0}}, 'time_now': '1616685310.655072', 'rate_limit_status': 118, 'rate_limit_reset_ms': 1616685310652, 'rate_limit': 120}
    
    import json
    
    json.loads(json.dumps(response, indent=4, sort_keys=True))
    

    这将为您提供以下输出:

    {'ext_code': '',
     'ext_info': '',
     'rate_limit': 120,
     'rate_limit_reset_ms': 1616685310652,
     'rate_limit_status': 118,
     'result': {'BTC': {'available_balance': 0.00208347,
       'cum_realised_pnl': 8.347e-05,
       'equity': 0.00208347,
       '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.00208347}},
     'ret_code': 0,
     'ret_msg': 'OK',
     'time_now': '1616685310.655072'}
    

    另一种解决方案是使用pprint

    import pprint
    
    pprint.pprint(response)
    

    这将为您提供以下输出:

    {'ext_code': '',
     'ext_info': '',
     'rate_limit': 120,
     'rate_limit_reset_ms': 1616685310652,
     'rate_limit_status': 118,
     'result': {'BTC': {'available_balance': 0.00208347,
                        'cum_realised_pnl': 8.347e-05,
                        'equity': 0.00208347,
                        '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.00208347}},
     'ret_code': 0,
     'ret_msg': 'OK',
     'time_now': '1616685310.655072'}
    

    【讨论】:

    • 感谢pprint 功能有效。但是,当我使用 json 函数时,它给了我错误:TypeError: Object of type RequestsResponseAdapter is not JSON serializable.
    • 你在哪个变量上应用了json函数?你的 sn-p 的余额?
    • 我将它应用于response 函数。它从网站 api 获取数据。
    • 您可能需要先将请求的输出转换为 json(RequestsResponseAdapter 类可能有方法 get_json 或类似的方法)
    【解决方案2】:

    导入 JSON,然后使用 json.dumps(balance, indent=4) 即可获得该格式。

    如果你想对它们进行排序,你可以添加 sort_keys=True 的关键字参数。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-01-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-04-30
      相关资源
      最近更新 更多