Python 3.x AttributeError str对象没有属性解码
我需要修复脚本代码。该脚本从公钥生成一个散列,并在python2中工作,但我需要它在python3中工作 我把密码从 当我从Python 3运行时,我得到一个错误:Python 3.x AttributeError str对象没有属性解码,python-3.x,utf-8,decode,Python 3.x,Utf 8,Decode,我需要修复脚本代码。该脚本从公钥生成一个散列,并在python2中工作,但我需要它在python3中工作 我把密码从 当我从Python 3运行时,我得到一个错误: 回溯(最近一次呼叫最后一次): 文件“D:/script.py”,第27行,在 hex_str=pubkey.decode('hex')) AttributeError:“str”对象没有属性“decode” 这是否回答了您的问题? import hashlib import base58 # ECDSA bitcoin Publ
回溯(最近一次呼叫最后一次):
文件“D:/script.py”,第27行,在
hex_str=pubkey.decode('hex'))
AttributeError:“str”对象没有属性“decode”
这是否回答了您的问题?
import hashlib
import base58
# ECDSA bitcoin Public Key
pubkey = '0450863ad64a87ae8a2fe83c1af1a8403cb53f53e486d8511dad8a04887e5b23522cd470243453a299fa9e77237716103abc11a1df38855ed6f2ee187e9c582ba6'
# See 'compressed form' at https://en.bitcoin.it/wiki/Protocol_documentation#Signatures
compress_pubkey = False
def hash160(hex_str):
sha = hashlib.sha256()
rip = hashlib.new('ripemd160')
sha.update(hex_str)
rip.update( sha.digest() )
print ( "key_hash = \t" + rip.hexdigest() )
return rip.hexdigest() # .hexdigest() is hex ASCII
if (compress_pubkey):
if (ord(pubkey[-2:].decode('hex')) % 2 == 0):
pubkey_compressed = '02'
else:
pubkey_compressed = '03'
pubkey_compressed += pubkey[2:66]
hex_str = pubkey_compressed.decode('hex')
else:
hex_str = pubkey.decode('hex')
# Obtain key:
key_hash = '00' + hash160(hex_str)
# Obtain signature:
sha = hashlib.sha256()
sha.update( key_hash.decode('hex') )
Traceback (most recent call last):
File "D: /script.py", line 27, in <module>
hex_str = pubkey.decode ('hex')
AttributeError: 'str' object has no attribute 'decode'