Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
我正在尝试在其中 x 是 for 循环中的变量的脚本中使用 binascii.hexlify(b'x')。目前,每次我运行我的脚本时,它都使用字符 x 而不是变量 x 存储的内容。
我正在使用 Python 3.7 32 位。
我假设您尝试过binascii.hexlify(x)但没有成功,因此您尝试将其放在b前面以将其转换为字节。这仅适用于字符串文字,不适用于变量。为此,您需要encode:
binascii.hexlify(x)
b
encode
binascii.hexlify(x.encode('utf-8'))
我不知道您需要传递什么编码参数encode,这取决于您对结果字符串所做的操作。不过utf-8是一个好的开始。
utf-8
您只需要: