python - 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
如何将字符串转换为其字节值?我
解决方案
您需要指定编码Latin
类型
>>> b'\x00\x01_\x97'.decode("Latin")
'\x00\x01_\x97'
>>> type(b'\x00\x01_\x97'.decode("Latin"))
<class 'str'>
>>>
推荐阅读
- javascript - 如何在按钮单击时在 EXTJS 中添加字段集
- ios - RSA 加密仅在运行 iOS 9 的 iPhone 4s 上失败
- java - 获取接口“FloatingPoint”的名称,而不是扩展的类名“DataTypeA”
- javascript - 什么是 PHP trait 的 nodejs 等价物
- objective-c - Objective-c中需要为JNI导入子框架头文件
- pkcs#11 - PKCS#11 C_CreateObject 因参数错误而失败
- android - 什么都不调整时如何获得键盘高度?
- google-chrome - 未打开的网页和 Chrome 扩展程序之间的消息传递
- angular - 如何在 Angular 5 server.ts 中获取 --env 变量的值?
- c# - 获取一个类的所有成员(道具、字段),在一个类中