相关疑难解决方法(0)

如何将python urandom转换为字符串?

如果我调用os.urandom(64),我会得到64个随机字节.参考将字节转换为Python字符串,我尝试过

a = os.urandom(64)
a.decode()
a.decode("utf-8")
Run Code Online (Sandbox Code Playgroud)

但得到了回溯错误,说明字节不在utf-8中.

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
UnicodeDecodeError: 'utf-8' codec can't decode byte 0x8b in position 0: invalid start byte
Run Code Online (Sandbox Code Playgroud)

用字节

b'\x8bz\xaf$\xb6\x93q\xef\x94\x99$\x8c\x1eO\xeb\xed\x03O\xc6L%\xe70\xf9\xd8
\xa4\xac\x01\xe1\xb5\x0bM#\x19\xea+\x81\xdc\xcb\xed7O\xec\xf5\\}\x029\x122
\x8b\xbd\xa9\xca\xb2\x88\r+\x88\xf0\xeaE\x9c'
Run Code Online (Sandbox Code Playgroud)

是否有一种全面的方法将这些字节解码为一些字符串表示?我正在生成sudo随机令牌以跟踪多个数据库引擎中的相关文档.

python string random byte python-3.x

29
推荐指数
3
解决办法
3万
查看次数

标签 统计

byte ×1

python ×1

python-3.x ×1

random ×1

string ×1