python递归实现字典中的Unicode对象encode成str
来源:互联网 发布:淘宝基础版全屏店招 编辑:程序博客网 时间:2024/06/05 15:50
Python自带的Json库会把json文件load成Unicode对象。如果想要变成str对象的话,就要自己去encode。个人认为这是python2.7中存在的一个坑,不过据说python3.x中此坑已修复。
下面上代码,是在网上找到的,亲测好用。
def byteify(input): if isinstance(input, dict): return {byteify(key): byteify(value) for key, value in input.iteritems()} elif isinstance(input, list): return [byteify(element) for element in input] elif isinstance(input, unicode): return input.encode('utf-8') else: return input
这个函数递归的把list和dict里的Unicode对象encode成str。
当然,这种方式是无奈之举。递归的效率我们都懂得,谁有好的方法,要留言告知哦,收下我的膝盖!
1 0
- python递归实现字典中的Unicode对象encode成str
- python的str,unicode对象的encode和decode方法
- python的str,unicode对象的encode和decode方法
- python的str,unicode对象的encode和decode方法
- python的str,unicode对象的encode和decode方法
- python的str,unicode对象的encode和decode方法
- python的str,unicode对象的encode和decode方法
- 【python】str,unicode对象的encode和decode方法
- python的str,unicode对象的encode和decode方法(转)
- python Unicode与str decode与encode 中文输出
- unicode .decode /str.encode方法
- python中的编码问题unicode, encode, decode
- python中的str与unicode处理方法
- Python 中的 str 与 unicode 编码处理
- Python中的str与unicode处理方法
- Python中的str与unicode处理方法
- Python中的str与unicode处理方法
- Python中的str与unicode处理方法
- 程序员为什么这么难啊。
- 面向对象六大原则
- keydown、keypress、keyup的使用
- 常见的登录注册错误类
- Cookie的应用---十天免登陆设计
- python递归实现字典中的Unicode对象encode成str
- Master code attach volume to VM
- 虚拟私有云:内部部署计算的替代解决方案
- 使用mysqlbinlog工具进行基于位置或时间点的数据恢复
- 实践---spring mvc框架搭建
- 微信小程序分享功能
- 计划
- 实现自适应页面的方法,适合APP开发
- git本地创建分支