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'的更多相关文章

  1. Django2.2报错 AttributeError: 'str' object has no attribute 'decode'

    准备将 Django 连接到 MySQL,在命令行输入命令 python manage.py makemigrations 后报错: AttributeError: 'str' object has ...

  2. Django项目与mysql交互进行数据迁移时报错:AttributeError: 'str' object has no attribute 'decode'

    问题描述 Django项目启动,当我们执行命令 python manage.py makemigrations 出现如下错误: File , in last_executed_query query ...

  3. python3.x运行的坑:AttributeError: 'str' object has no attribute 'decode'

    1.Python3.x和Python2.X版本有一些区别,我遇到了两个问题如下: a.第一个报:mysqlclient 1.3版本不对: 解决办法:注释掉这行即可: b.第二个报:字符集的问题: 报错 ...

  4. 执行: python manage.py makemigrations报AttributeError: 'str' object has no attribute 'decode'

    找到错误代码(line146):query = query.encode(errors='replace') 解决方法:把decode改为encode即可.

  5. AttributeError: 'str' object has no attribute 'decode'

    ue = e.decode('latin-1')修改为: ue = e.encode('ascii', 'strict')

  6. Python PyInstaller 打包报错:AttributeError: 'str' object has no attribute 'items'

    pyinstaller打包时报错:AttributeError: 'str' object has no attribute 'items' 网上查询,可能是setuptools比较老: 更新一下 p ...

  7. 【Python-遇到的Error】AttributeError: 'str' object has no attribute 'input_text'

    学习类的实例化的时候遇到了AttributeError: 'str' object has no attribute 'input_text', 以下是报错的代码及修改正确的代码. class shu ...

  8. Django 运行报异常:AttributeError: 'str' object has no attribute 'get'

    Technorati Tags: Python,Django,Web 在使用django.contrib.auth用户机制进行用户的验证.登录.注销操作时,遇到这个异常. 首先是写了一个登录的视图,要 ...

  9. python2 'str' object has no attribute 'decode'

    '.decode('hex') 上述代码,报错: 'str' object has no attribute 'decode' 查找原因: https://stackoverflow.com/ques ...

随机推荐

  1. EwoMail开源邮件服务器软件搭建

    EwoMail开源邮件服务器软件简介 EwoMail是基于Linux的开源邮件服务器软件,集成了众多优秀稳定的组件,是一个快速部署.简单高效.多语言.安全稳定的邮件解决方案,帮助你提升运维效率,降低 ...

  2. 核心task

    由于Ant具有跨平台的特性,因此编写Ant生成文件时可能会失去一些灵活性.为了弥补这个不足,Ant提供了一个“exec”核心task,允许执行特定操作系统上的命令.

  3. 如何用python批量生成真实的手机号码

    前言 文的文字及图片来源于网络,仅供学习.交流使用,不具有任何商业用途,版权归原作者所有,如有问题请及时联系我们以作处理. 作者:Python测试社区 1目 标 场 景 平时在工作过程中,偶尔会需要大 ...

  4. 068.Python框架Django之DRF视图集使用

    一 视图集与路由的使用 使用视图集ViewSet,可以将一系列逻辑相关的动作放到一个类中: list() 提供一组数据 retrieve() 提供单个数据 create() 创建数据 update() ...

  5. mac上安装htop

    对于经常在mac上使用top命令的童鞋来说,htop会是一个更加好看和更加好用的命令,下面就是在mac上安装htop的步骤 1.首先去htop官网去下载,我下的是最新的2.2.0版本,网址是https ...

  6. NGINX 类漏洞 整理记录

    简单介绍NGINX: Nginx是一款轻量级的Web 服务器/反向代理服务器及电子邮件(IMAP/POP3)代理服务器,在BSD-like 协议下发行. 其特点是占有内存少,并发能力强,nginx的并 ...

  7. Spark-BlockManager

    简单说明 BlockManager是管理整个Spark运行时数据的读写,包含数据存储本身,在数据存储的基础之上进行数据读写.由于Spark是分布式的,所有BlockManager也是分布式的,Bloc ...

  8. tp5 -- join注意事项

    使用数据库关联查询的时候,有时候会避免不了两个表格字段名称都一样的尴尬, 这时候管理查询出来的只有其中一个表格字段名称的数据,因为在相同字段名称的情况下,数据会自动覆盖. 这时候,我们只需要给其中一个 ...

  9. iscsi的工作原理与优化(2)

    2.1 iSCSI协议模型,iscsi[会话层协议,即应用协议] iSCSI使用TCP/IP协议在不稳定网络上进行可靠的数据传输.iSCSI层和标准SCSI集在协议栈中的位置如图1所示.iSCSI层包 ...

  10. How to change the header background color of a QTableView

    You can set the style sheet on the QTableView ui->tableView->setStyleSheet("QHeaderView:: ...