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 ...
随机推荐
- csu - 1566: The Maze Makers (bfs)
http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1566 题意还是蛮难懂的,至少对于我来说,需要认真读题. 输入矩阵的每一个数字换成2进制后,顺时针围 ...
- nyoj_205_求余数_201404271630
求余数 时间限制:1000 ms | 内存限制:65535 KB 难度:3 描述 现在给你一个自然数n,它的位数小于等于一百万,现在你要做的就是求出这个数除10003之后的余数 输入 第一 ...
- Java DynamoDB 增加、删除、修改、查询
准备jar包 <dependency> <groupId>com.amazonaws</groupId> <artifactId>aws-java-sd ...
- scp、paramiko、rsync复制文件的区别
1.paramiko只能复制文件,而不能复制目录,复制时,已经存在的会被覆盖;要想复制目录,只能把目录里的文件一个一个复制过去 2.scp可以复制文件.目录,复制时,已经存在的会被覆盖:可以模糊匹配: ...
- 条款五:对应的new和delete要采用相同的形式
string *stringarray = new string[100]; ... delete stringarray; 上述程序的运行情况将是不可预测的.至少,stringarray指向的100 ...
- linux句柄泄露问题查看
背景: 我们在开发linux在线server的时候常常会遇会句柄泄露的问题.由于在linux系统设计里面遵循一切都是文件的原则.即磁盘文件.文件夹.网络套接字.磁盘.管道等,全部这些都是文件.在我们进 ...
- 中国第二代身份证验证js代码
以下这部分代码截取自盛大的某个网页.详细我就不给url了.以下是相应的js代码: iW = new Array(7,9,10,5,8,4,2,1,6,3,7,9,10,5,8,4,2,1); iSum ...
- c++运算符重载以及一些基本概念
c++primer第四版435 1.赋值( = ), 下标( [ ] ) ,调用 ( ( ) ), 成员訪问箭头 (->)等操作符必须定义为成员,定义为非成员时,编译器报错 2. 像赋值一样 ...
- 学习Flash Builder编程的准备工作
1. 下载教科书,Essential ActionScript 3.0或者Programming ActionScript 3.0.这将是你的很好的老师.英语不灵的买一本中文版本. 2. 安装Flas ...
- MFC ListControl技巧汇总
转自:http://hi.baidu.com/qi_xian/blog/item/1971aa22da89ada24723e856.html 以下未经说明,listctrl默认view 风格为repo ...