python2 'str' object has no attribute 'decode'
''.decode('hex')
上述代码,报错:
'str' object has no attribute 'decode'
查找原因:
https://stackoverflow.com/questions/29030725/str-object-has-no-attribute-decode
You cannot decode string objects; they are already decoded. You'll have to use a different method.
You can use the codecs.decode()
function to apply hex
as a codec:
>>> import codecs
>>> codecs.decode('ab', 'hex')
b'\xab'
This applies a Binary transform codec; it is the equivalent of using the base64.b16decode()
function, with the input string converted to uppercase:
>>> import base64
>>> base64.b16decode('AB')
b'\xab'
You can also use the binascii.unhexlify()
function to 'decode' a sequence of hex digits to bytes:
>>> import binascii
>>> binascii.unhexlify('ab')
b'\xab'
Either way, you'll get a bytes
object.
使用第二种方式解决了:
>>> import base64
>>> base64.b16decode('AB')
python2 'str' object has no attribute 'decode'的更多相关文章
- python3.x运行的坑:AttributeError: 'str' object has no attribute 'decode'
1.Python3.x和Python2.X版本有一些区别,我遇到了两个问题如下: a.第一个报:mysqlclient 1.3版本不对: 解决办法:注释掉这行即可: b.第二个报:字符集的问题: 报错 ...
- 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 ...
- 解决编码问题:AttributeError: 'str' object has no attribute 'decode'
1. 问题发现: 出现:读取文件,对其进行解码,出现错误,AttributeError: 'str' object has no attribute 'decode' 解释:属性错误,str对象不包含 ...
- AttributeError: 'str' object has no attribute 'decode'
ue = e.decode('latin-1')修改为: ue = e.encode('ascii', 'strict')
- 执行: python manage.py makemigrations报AttributeError: 'str' object has no attribute 'decode'
找到错误代码(line146):query = query.encode(errors='replace') 解决方法:把decode改为encode即可.
- 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 ...
- python自动化测试,读取excal数据报"'str' object has no attribute 'items'"问题解决
通过python进行自动化测试,为了方便,对代码和数据进行了分离,此处把测试数据放到了excal表格中.requests.post请求时报"'str' object has no attri ...
随机推荐
- I - Balancing Act POJ - 1655
Consider a tree T with N (1 <= N <= 20,000) nodes numbered 1...N. Deleting any node from the t ...
- codevs——1013 求先序排列
1013 求先序排列 2001年NOIP全国联赛普及组 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 黄金 Gold 题解 题目描述 Description 给出 ...
- io计算
http://www.cnblogs.com/yalong_xiang/archive/2011/11/15/2249530.html ぬ儱←OWEN★ windows下如何查看磁盘IO性能 复制 ...
- js下载
下载用ajax不好使,得用表单提交的方式 download:function(url,paramObj){ var doc = document; //使用一个隐藏的form表单执行提交,没有则创建 ...
- Office 如何打印彩色照片能取得较好的效果
1 如下图所示,随便打开一个照片,点击打印,纸张大小,质量,纸张类型如下所示. 2 这样打印的设置还是不够的,因为"高级光面纸"或者类似的纸张类型,会把色彩浓度调大,相对于普通 ...
- DMA32映射问题
近期在调试PCIe的行情加速卡的驱动.当中使用DMA在CPU和FPGA间数据传输. 最開始使用的是低16M的DMA ZONE的内存,用slab分配器的kmalloc分配获取.但因为最新的需求,须要使用 ...
- ajax请求锁屏功能
我们有时候在进行ajax请求的时候希望页面不允许点击,等请求结束之后才可以进行点击,那么可以写: $(".cloudos-container").ajaxStart($.block ...
- 实现日、周、月排行统计 sql
在如今很多系统中,都需要进行日.周.月排行统计,但是在网上寻找 了一番,发现很多都是相对的周.月排行,即周排行则用当前时间减去7天.这样我个人认为并不恰当.如月排行中,假设今天是4月22日,则从3月2 ...
- MySQL具体解释(21)------------缓存參数优化
数据库属于 IO 密集型的应用程序.其主要职责就是数据的管理及存储工作. 而我们知道,从内存中读取一个数据库的时间是微秒级别,而从一块普通硬盘上读取一个IO是在毫秒级别,二者相差3个数量级.所以,要优 ...
- Python爬虫(一):基本概念
网络爬虫的定义 网络爬虫(Web Spider.又被称为网页蜘蛛.网络机器人,又称为网页追逐者),是一种依照一定的规则,自己主动的抓取万维网信息的程序或者脚本.另外一些不常使用的名字 ...