python2 'str' object has no attribute 'decode'
2024-08-25 23:34:27
''.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')
最新文章
- SQL 约束
- [转]在 Web 项目中应用 Apache Shiro
- android 学习资源总结
- JavaScript基础插曲—获取标签,插入元素,操作样式
- COJ 删除数字
- bzoj 1012 维护一个单调数列
- javaSE第十三天
- android 多语言版本开发
- php错误处理和异常处理
- Linux目录结构及文件基础操作
- Windows Phone 8初学者开发—第14部分:在运行时绑定到真实的数据
- SQL server 表数据改变触发发送邮件
- WordPress 邮箱防抓取
- HTML做的网页 如何使当前页面跳转到另一页面锚点处
- Expm 10_2 实现Ford-Fulkerson算法,求出给定图中从源点s到汇点t的最大流,并输出最小割。
- awd入门教程
- 遍历一个Set的方法只有一个:迭代器(interator)。
- Vue 中使用 viewerjs
- 国外青少年最喜爱的聊天 app,竟然是 Google Docs
- Oracle的简单的创建dblink以及进行数据迁移的方法
热门文章
- Codeforces 645A Amity Assessment【八数码】
- 51Nod——T 1686 第K大区间
- Linux系统备份还原工具4(rsync/远程数据同步工具)
- pymongo collection.save 问题
- hbase definitive guide 笔记
- Serv-u 10.3 的图文安装教程及使用方法
- 踩坑录-mysql不允许远程连接(错误码:1130) Host'xxx.xxx.xxx.xxx' is not allowed to connect to this MySQL server“
- Elasticsearch学习系列之mapping映射
- hive中使用正則表達式不当导致执行奇慢无比
- [Canvas画图] 藏图阁(16) 人体穴位