項(xiàng)目運(yùn)行失敗,但是不知哪里原因?所以新人最重要學(xué)會(huì)添加錯(cuò)誤打印
狀態(tài):想把Python代碼跑起來(lái),但是結(jié)果并不理想?,F(xiàn)在的問(wèn)題是,我都不知道失敗的原因是什么。所以我們?cè)趯懘a的時(shí)候,一定要學(xué)會(huì)添加報(bào)錯(cuò)打印,方便后續(xù)debug跟蹤
try:
xxxxxx
except Exception as e:
print('報(bào)錯(cuò):' + e)
報(bào)錯(cuò)can only concatenate str (not “NoneType“) to str
后面debug時(shí)候發(fā)現(xiàn),在print打印的時(shí)候報(bào)類型不對(duì)的錯(cuò)誤。websocket建立連接后,打回的message是str字符串,在取出字符串的時(shí)候,發(fā)生的報(bào)錯(cuò)。所以我先將str轉(zhuǎn)為了對(duì)象json.loads(),然后強(qiáng)轉(zhuǎn)需要的類型,但是打印的時(shí)候必須轉(zhuǎn)為str類型打印
try:
rsl = json.loads(message)
if rsl["e"] == "depthUpdate":
ask = float(rsl['a'][0][0])
bid = float(rsl['b'][0][0])
print('binance ask', ask, 'binance bid', bid)
if self.lastA == ask and self.lastB == bid:
return
else:
self.lastA = ask
self.lastB = bid
self.queue.put([[ask, bid], 0])
else:
print("不是depthUpdate")
print(rsl)
except Exception as e:
print('binance行情報(bào)錯(cuò):' + e)