【发布时间】:2014-12-28 23:51:10
【问题描述】:
我正在尝试获得一个 private_key 所以,我尝试了这个:
private_key = os.urandom(32).encode('hex')
但它会抛出这个错误:
AttributeError: 'bytes' object has no attribute 'encode'
所以我检查了问题并解决了这个问题,在 Python3x 中,字节只能解码。然后我将其更改为:
private_key = os.urandom(32).decode('hex')
但是现在它抛出了这个错误:
LookupError: 'hex' is not a text encoding; use codecs.decode() to handle arbitrary codecs
我真的不明白为什么。当我在上次错误后尝试此操作时;
private_key = os.urandom(32).codecs.decode('hex')
上面写着
AttributeError: 'bytes' 对象没有属性 'codecs'
所以我卡住了,我该怎么做才能解决这个问题?我听说这是在 Python 2x 中工作的,但我需要在 3x 中使用它。
【问题讨论】:
标签: python string hex ascii decode