在Python 3.5中从dict中提取值

时间:2017-08-19 11:35:35

标签: python-3.x dictionary

这是我现在的代码:

import hashlib
import hmac
import time
import requests
import datetime


def send_msg(msg, env='prod'):
    if env == 'prod':
        BLINKTRADE_API_URL = 'https://api.blinktrade.com'
        BLINKTRADE_API_VERSION = 'v1'
    else:
        BLINKTRADE_API_URL = 'https://api.testnet.blinktrade.com'
        BLINKTRADE_API_VERSION = 'v1'
        TIMEOUT_IN_SECONDS = 10
    key = 'mykeyhere'
    secret = 'mysecrethere'
    secret2 = bytearray(secret, 'utf8') #turn secret into bytearray
    dt = datetime.datetime.now()
    nonce = str(int((time.mktime(dt.timetuple()) + dt.microsecond/1000000.0) * 1000000))
    nonce = nonce.encode("utf8")
    signature = hmac.new(secret2,  nonce, digestmod=hashlib.sha256).hexdigest()
    headers = {
        'user-agent': 'blinktrade_tools/0.1',
        'Content-Type': 'application/json',       # You must POST a JSON message
        'ApiKey': key,                            # Your APIKey
        'Nonce': nonce,                           # The nonce must be an integer, always greater than the previous one.
        'Signature': signature                    # Use the API Secret  to sign the nonce using HMAC_SHA256 algo
    }
    url = '%s/tapi/%s/message' % (BLINKTRADE_API_URL, BLINKTRADE_API_VERSION)
    return requests.post(url, json=msg, verify=True, headers=headers,).json()



# Request Balance
msg_balance = {
    "MsgType": "U2",
    "BalanceReqID": 1,
}

msg_orders = {
    "MsgType": "U4",
    "OrdersReqID": 930460
}

print(send_msg(msg_balance))

打印:

{'Responses': [{'ClientID': 90829382, 'MsgType': 'U3', '4': {'BTC_locked': 737697, 'BTC': 737697, 'BRL': 746655, 'BRL_locked': 0}, 'BalanceReqID': 1}], 'Description': 'OK', 'Status': 200}

我需要的是获得“BRL”,“BTC”这些价值观。经过4个小时的研究,我找不到解决方案。

我尝试过:

data = (send_msg(msg_balance))

print(data['Responses'])

到目前为止一直很好,但我无法设法获得BTC和BRL值。如何提取这些值? 感谢您的耐心等待。

1 个答案:

答案 0 :(得分:0)

Comparator<Person> ascNameDescAgeComp = 
   comparing(Person::getName)
   .thenComparing(comparingInt(Person::getAge).reversed());
Collections.sort(persons, ascNameDescAgeComp );

这解决了我的问题。我只是不明白为什么[-1]而不是[1]。