python-3.x - 如何将字符串类型更改为字节串?

标签 python-3.x

例如,我有 '\x87',我想要 b'\x87'
我知道,存在 .encode(),但是当我执行 ('\x87').encode() 时,我得到 b'\xc2\x87' 而不是 b'\x87'
有没有办法告诉 python 应该将给定的字符串解释为字节串,而不可能以任何方式更改它?

最佳答案

试试这个。

my_str = '\x87'
my_str_as_bytes = my_str.encode(encoding='latin')

关于python-3.x - 如何将字符串类型更改为字节串?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59078609/

相关文章:

django - 如何检查自用户上次登录 Django 以来的新帖子

python - 更改具有条件的数据框列

python - 如何用header和body构造python post请求

python - "Pythonic"只要基于前一个元素的条件为真,就可以从可迭代对象中返回元素

Python 3.3 与 Python 3.2 问题

python-3.x - struct.error : unpack requires a buffer of 4 bytes

python-3.x - Python win32 ShellExecute 错误 31 : 'A device attached to the system is not functioning.'

python - 为什么在Python类的__init__中使用self.foo = self.foo?

Python glob.glob 总是返回空列表

python - 如何根据列值的长度过滤数据框行