亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何修復 AttributeError:'bytes' 對象沒有屬性 'encode'?

如何修復 AttributeError:'bytes' 對象沒有屬性 'encode'?

LEATH 2022-01-11 16:06:10
這是我的代碼z = (priv.to_string().encode('hex')) ,我收到了這個錯誤:"AttributeError: 'bytes' object has no attribute 'encode'"看起來我錯過了在代碼之后顯示“編碼”的東西:z = (priv.to_string().
查看完整描述

2 回答

?
萬千封印

TA貢獻1891條經驗 獲得超3個贊

這里有兩個問題:

  • 您正在使用priv.to_string()(這不是內置方法)而不是str(priv)

  • 'hex'已在 Python 3 中作為編碼被刪除,因此str(priv).encode('hex')您將收到以下錯誤:LookupError: 'hex' is not a text encoding; use codecs.encode()to handle arbitrary codecs

但是,從 Python 3.5 開始,您可以簡單地執行以下操作:

priv.hex()

priv作為一個字節的字符串。

例子:

priv = b'test'
print(priv.hex())

輸出:

74657374


查看完整回答
反對 回復 2022-01-11
?
MMTTMM

TA貢獻1869條經驗 獲得超4個贊

在版本 3.5 之前的 Python3 系統上,您可以from binascii import hexlify使用hexlify(priv.to_string())


查看完整回答
反對 回復 2022-01-11
  • 2 回答
  • 0 關注
  • 907 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號