Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 3.x AttributeError str对象没有属性解码_Python 3.x_Utf 8_Decode - Fatal编程技术网

Python 3.x AttributeError str对象没有属性解码

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

我需要修复脚本代码。该脚本从公钥生成一个散列,并在python2中工作,但我需要它在python3中工作

我把密码从

当我从Python 3运行时,我得到一个错误:

回溯(最近一次呼叫最后一次):
文件“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'