我试图检查一个简单的标志是真还是假,但我的IF语句失败了,它没有给我正确的结果,希望有人可以提供帮助!
我使用(在python3.6中)这个网址
https://bittrex.com/api/v1.1/public/getticker?market=usdt-btc
获取请求及其应返回的数据如下所示(来自浏览器中的URL):
{"success":true,"message":"","result":{"Bid":18362.00000000,"Ask":18399.00000000,"Last":18362.00000000}}
这是我的计划:
import sys
import time
import requests
import json
import os
BTCtick = 'https://bittrex.com/api/v1.1/public/getticker?market=usdt-btc'
reqBTC = requests.get('https://bittrex.com/api/v1.1/public/getticker? market=usdt-btc').text
BTCdata = json.loads(reqBTC)
testResult = BTCdata['success']
print("=============================")
print("success content: ", BTCdata['success'])
if (testResult == 'True'):
print("IF: success flag is TRUE")
else:
print("IF: success flag is FALSE")
print("=============================")
print("result content: ", BTCdata['result'])
print("Bid: ", BTCdata['result'] ['Bid'], sep='')
print("Ask: ", BTCdata['result'] ['Ask'], sep='')
print("Last: ", BTCdata['result'] ['Last'], sep='')
print("=============================")
exit()
控制台中的输出如下所示:
=============================
success content: True
IF: success flag is FALSE
=============================
result content: {'Bid': 18420.0, 'Ask': 18439.99999999, 'Last': 18439.99999999}
Bid: 18420.0
Ask: 18439.99999999
Last: 18439.99999999
=============================
知道我在这里失踪的是什么吗?我确定这很简单 请帮助我,我对python有些新意见并找出错误,过去两天我一直在阅读,但找不到问题!
答案 0 :(得分:2)
json.loads()
会将true
转换为Python的布尔True
(conversion table for the reference)。您只需要检查testResult
值的真实性,而不是将其与字符串'True'
进行比较:
if testResult:
print("IF: success flag is TRUE")
else:
print("IF: success flag is FALSE")
换句话说,以下是您将testResult
与'True'
字符串进行比较时发生的情况:
In [1]: testResult = True
In [2]: testResult == 'True'
Out[2]: False