如何在python

时间:2019-02-18 18:56:15

标签: python json database

嗨,我正在尝试使交易机器人的其他分钟交易量达到一定水平。但是有时候我拿到我的api时我得到的数字太少了 就像82.7641093一样。我想更改“ volume_traded”值。当它低于1500时,我想要我的json,因为它被计为1500

  url_XRP5MIN = 
'https://rest.coinapi.io/v1/ohlcv/POLONIEX_SPOT_XRP_BTC/latest? 
period_id=5MIN'
headers = {'X-CoinAPI-Key': 'YOUR APİ KEY'}
responseXRP5 = requests.get(url_XRP5MIN, headers=headers)

XRPLAST5Volume=responseXRP5.json()[0]['volume_traded']
print(XRPLAST5Volume)

"time_period_start": "2019-02-18T18:15:00.0000000Z",
"volume_traded": 171673.27542305,



"time_period_start": "2019-02-18T18:30:00.0000000Z",
"volume_traded": 82.76410936,

2 个答案:

答案 0 :(得分:0)

import json, requests

url = 'https://rest.coinapi.io/v1/ohlcv/POLONIEX_SPOT_XRP_BTC/latest?'

headers = {'X-CoinAPI-Key': 'YOUR APİ KEY'}
response = requests.get(url, headers=headers)

result = response.json()

print(result[0]['volume_traded'])
if float(result[0]['volume_traded']) <= float(1500):
    # do whatever like reset the value
    result[0]['volume_traded'] = 2500

print(json.dumps(result, sort_keys=True, indent=4))
# notice the new value we set

答案 1 :(得分:0)

您可以按以下方式访问JSON值

print your_dict['key']

因此,要更改值,您可以执行以下操作

if float(your_dict['volume_traded']) < 1500:
    your_dict['volume_traded'] = 1500