解决编码问题:AttributeError: 'str' object has no attribute 'decode'
1. 问题发现:
出现:读取文件,对其进行解码,出现错误,AttributeError: 'str' object has no attribute 'decode'
解释:属性错误,str对象不包含‘decode’属性。
2.原因解释:
出现问题原因:str与bytes表示的是两种数据类型,str为字符串型,bytes为字节型。对str编码encode得到bytes,
对bytes解码得到str,两者互为转换。而上面出现问题的原因是对str字符串使用了解码,显然是猪头不对马尾。
3.解决方法:
解决办法:删除decode(‘utf-8’)
4.代码演示:
txt = '你好,shiyi,很感谢你陪伴我的日子'
#str->bytes encode
txt = txt.encode('utf-8')
print(type(txt))
#bytes->str decode
txt = txt.decode('utf-8')
print(type(txt))
5.结果展示:
<class 'bytes'>
<class 'str'>
Process finished with exit code 0
解决编码问题:AttributeError: 'str' object has no attribute 'decode'的更多相关文章
- Django2.2报错 AttributeError: 'str' object has no attribute 'decode'
准备将 Django 连接到 MySQL,在命令行输入命令 python manage.py makemigrations 后报错: AttributeError: 'str' object has ...
- Django项目与mysql交互进行数据迁移时报错:AttributeError: 'str' object has no attribute 'decode'
问题描述 Django项目启动,当我们执行命令 python manage.py makemigrations 出现如下错误: File , in last_executed_query query ...
- python3.x运行的坑:AttributeError: 'str' object has no attribute 'decode'
1.Python3.x和Python2.X版本有一些区别,我遇到了两个问题如下: a.第一个报:mysqlclient 1.3版本不对: 解决办法:注释掉这行即可: b.第二个报:字符集的问题: 报错 ...
- 执行: python manage.py makemigrations报AttributeError: 'str' object has no attribute 'decode'
找到错误代码(line146):query = query.encode(errors='replace') 解决方法:把decode改为encode即可.
- AttributeError: 'str' object has no attribute 'decode'
ue = e.decode('latin-1')修改为: ue = e.encode('ascii', 'strict')
- Python PyInstaller 打包报错:AttributeError: 'str' object has no attribute 'items'
pyinstaller打包时报错:AttributeError: 'str' object has no attribute 'items' 网上查询,可能是setuptools比较老: 更新一下 p ...
- 【Python-遇到的Error】AttributeError: 'str' object has no attribute 'input_text'
学习类的实例化的时候遇到了AttributeError: 'str' object has no attribute 'input_text', 以下是报错的代码及修改正确的代码. class shu ...
- Django 运行报异常:AttributeError: 'str' object has no attribute 'get'
Technorati Tags: Python,Django,Web 在使用django.contrib.auth用户机制进行用户的验证.登录.注销操作时,遇到这个异常. 首先是写了一个登录的视图,要 ...
- python2 'str' object has no attribute 'decode'
'.decode('hex') 上述代码,报错: 'str' object has no attribute 'decode' 查找原因: https://stackoverflow.com/ques ...
随机推荐
- LCS(记录路径)+LIS+LCIS
https://blog.csdn.net/someone_and_anyone/article/details/81044153 当串1 和 串2 的位置i和位置j匹配成功时, dp[i][j]=d ...
- 从零开始学AB测试:基础篇
什么是AB测试? 通俗点理解,AB测试就是比较两个东西好坏的一套方法,这种A和B的比较在我们的生活和人生中非常常见,所以不难理解.具体到AB测试这个概念,它和我们比较哪个梨更大.比较哪个美女更漂亮.比 ...
- 详解 字符串—— String、StringBuffer 与 StringBuilder
本来这篇博文的内容,本人打算在之后的代码中一点一点通过实例讲解的,但是,本人发现,其实这里的知识点还是蛮重要的. 并且,字符串类型,在任何的程序语言中都是被认真对待的,所以,今天专门写一篇博文来介绍一 ...
- python数据分析工具——Pandas、StatsModels、Scikit-Learn
Pandas Pandas是 Python下最强大的数据分析和探索工具.它包含高级的数据结构和精巧的工具,使得在 Python中处理数据非常快速和简单. Pandas构建在 Numpy之上,它使得以 ...
- Java 网络编程 -- 基于TCP 模拟多用户登录
Java TCP的基本操作参考前一篇:Java 网络编程 – 基于TCP实现文件上传 实现多用户操作之前先实现以下单用户操作,假设目前有一个用户: 账号:zs 密码:123 服务端: public c ...
- 初学者的Pygame安装教程
最近在自学python,在看完了些基础知识之后,准备写个小项目[外星人入侵],这个项目需要安装pygame. 所以就在网上找到了两个下载地址https://bitbucket.org/pygame/p ...
- 如何打开 Visual Studio 的 Dump,适用于调试 appcrash,exception
https://keithbabinec.com/2018/06/12/how-to-capture-and-debug-net-application-crash-dumps-in-windows/ ...
- Zabbix备份数据文件
mysql自带的工具mysqldump,当数据量大了之后进行全备所花的时间比较长,这样将会造成数据库的锁读.从而zabbix服务的监控告警不断,想着做下配置文件的备份.刚好有这么个脚本.满足了需求. ...
- Scala的存在类型
Scala的存在类型 存在类型也叫existential type,是对类型做抽象的一种方法.可以在你不知道具体类型的情况下,就断言该类型存在. 存在类型用_来表示,你可以把它看成java中的?. 下 ...
- weblogic创建域
一.webLogic服务域创建 https://blog.csdn.net/github_38922197/article/details/75097320