首页 > 解决方案 > UnicodeDecodeError:“utf-8”编解码器无法解码位置 3 中的字节 0x97:无效的起始字节

问题描述

我正在使用 Python 3,但在将格式从字符串转换为字节时在服务器日志中发现了这个错误

b'\x00\x01_\x97'.decode()
Traceback (most recent call last):
  File "<pyshell#5>", line 1, in <module>
    b'\x00\x01_\x97'.decode()
UnicodeDecodeError: 'utf-8' codec can't decode byte 0x97 in position 3: invalid start byte

如何将字符串转换为其字节值?我

标签: pythonhextypeconverter

解决方案


您需要指定编码Latin类型

>>> b'\x00\x01_\x97'.decode("Latin")
   '\x00\x01_\x97'
>>> type(b'\x00\x01_\x97'.decode("Latin"))
   <class 'str'>
>>> 

推荐阅读