【问题标题】:UnicodeDecodeError: 'utf-8' codec can't decode byte 0x97 in position 3: invalid start byteUnicodeDecodeError:“utf-8”编解码器无法解码位置 3 中的字节 0x97:无效的起始字节
【发布时间】:2018-05-20 07:14:23
【问题描述】:

我正在使用 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

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

【问题讨论】:

  • 你用的是哪个版本的python?
  • 它的 Python 3.6.3
  • 你的输出应该是怎样的?属于哪一种?
  • 我想从这行数据中删除 b''
  • 使用“utf-8”或“utf-16”的编码是什么?

标签: python hex typeconverter


【解决方案1】:

您需要通过Latin指定编码类型

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

【讨论】:

  • @Doctor PC:请确认您的错误是否解决
  • @Doctor PC:这是你所期待的吗?
猜你喜欢
  • 2022-05-30
  • 2021-12-01
  • 2016-05-13
  • 2017-05-03
  • 2020-02-06
  • 2021-10-22
  • 1970-01-01
  • 1970-01-01
  • 2018-03-14
相关资源
最近更新 更多