Skip to content
Advertisement

Struggling to print specific content from a script in python

from the following code:

print(r.content)
print("Bitstamp balance: " + r.content['btc_balance'] + " BTC")

I am receiving the following response:

❯ python3 bitcoin.py
b'{"bch_available": "0.00000000", "bch_balance": "0.00000000", "bch_reserved": "0.00000000", "bch_withdrawal_fee": "0.00010000", "bchbtc_fee": "0.500", "bcheur_fee": "0.500", "bchgbp_fee": "0.500", "bchusd_fee": "0.500", "btc_available": "0.00000000", "btc_balance": "0.04064798", "btc_reserved": "0.04064798", "btc_withdrawal_fee": "0.00050000", "btceur_fee": "0.500", "btcgbp_fee": "0.500", "btcpax_fee": "0.500", "btcusd_fee": "0.500", "btcusdc_fee": "0.500", "eth_available": "0.00000000", "eth_balance": "0.00000000", "eth_reserved": "0.00000000", "eth_withdrawal_fee": "0.04000000", "ethbtc_fee": "0.500", "etheur_fee": "0.500", "ethgbp_fee": "0.500", "ethpax_fee": "0.500", "ethusd_fee": "0.500", "ethusdc_fee": "0.500", "eur_available": "0.00", "eur_balance": "0.00", "eur_reserved": "0.00", "eurusd_fee": "0.050", "gbp_available": "0.00", "gbp_balance": "0.00", "gbp_reserved": "0.00", "gbpeur_fee": "0.050", "gbpusd_fee": "0.050", "link_available": "0.00000000", "link_balance": "0.00000000", "link_reserved": "0.00000000", "link_withdrawal_fee": "0.25000000", "linkbtc_fee": "0.500", "linketh_fee": "0.500", "linkeur_fee": "0.500", "linkgbp_fee": "0.500", "linkusd_fee": "0.500", "ltc_available": "0.00000000", "ltc_balance": "0.00000000", "ltc_reserved": "0.00000000", "ltc_withdrawal_fee": "0.00100000", "ltcbtc_fee": "0.500", "ltceur_fee": "0.500", "ltcgbp_fee": "0.500", "ltcusd_fee": "0.500", "omg_available": "0.00000000", "omg_balance": "0.00000000", "omg_reserved": "0.00000000", "omg_withdrawal_fee": "0.75000000", "omgbtc_fee": "0.500", "omgeur_fee": "0.500", "omggbp_fee": "0.500", "omgusd_fee": "0.500", "pax_available": "0.00000", "pax_balance": "0.00000", "pax_reserved": "0.00000", "pax_withdrawal_fee": "3.00000", "paxeur_fee": "0.050", "paxgbp_fee": "0.050", "paxusd_fee": "0.050", "usd_available": "0.00", "usd_balance": "0.00", "usd_reserved": "0.00", "usdc_available": "0.00000", "usdc_balance": "0.00000", "usdc_reserved": "0.00000", "usdc_withdrawal_fee": "3.00000", "usdceur_fee": "0.050", "usdcusd_fee": "0.050", "xlm_available": "0.00000000", "xlm_balance": "0.00000000", "xlm_reserved": "0.00000000", "xlm_withdrawal_fee": "0.00500000", "xlmbtc_fee": "0.500", "xlmeur_fee": "0.500", "xlmgbp_fee": "0.500", "xlmusd_fee": "0.500", "xrp_available": "0.00000000", "xrp_balance": "0.00000000", "xrp_reserved": "0.00000000", "xrp_withdrawal_fee": "0.02000000", "xrpbtc_fee": "0.500", "xrpeur_fee": "0.500", "xrpgbp_fee": "0.500", "xrppax_fee": "0.500", "xrpusd_fee": "0.500"}'
Traceback (most recent call last):
  File "bitcoin.py", line 60, in <module>
    print("Bitstamp balance: " + r.content['btc_balance'] + " BTC")
TypeError: byte indices must be integers or slices, not str

But I am struggling the with error message. I’m simply trying to return the btc_balance from the script, but it’s not letting me. And what are byte indicies?

Advertisement

Answer

r.content returns a byte object. So you can decode the bytes to string object first and then use the json module to deserialize to a Python object and access the specific key btc_balance

>>> json.loads(r.content.decode('utf-8'))['btc_balance']

If r here is requests Reponse object, then it has a .json method which will automatically deserialize the response for you. So you could just do,

>>> r.json()['btc_balance']
Advertisement