byteify函数

Python自带的Json库会把json文件load成Unicode对象。

如果想要变成str对象的话,就要自己去encode。


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。

抄袭过来的

import json

def json_load_byteified(file_handle):
return _byteify(
json.load(file_handle, object_hook=_byteify),
ignore_dicts=True
) def json_loads_byteified(json_text):
return _byteify(
json.loads(json_text, object_hook=_byteify),
ignore_dicts=True
) def _byteify(data, ignore_dicts = False):
# if this is a unicode string, return its string representation
if isinstance(data, unicode):
return data.encode('utf-8')
# if this is a list of values, return list of byteified values
if isinstance(data, list):
return [ _byteify(item, ignore_dicts=True) for item in data ]
# if this is a dictionary, return dictionary of byteified keys and values
# but only if we haven't already byteified it
if isinstance(data, dict) and not ignore_dicts:
return {
_byteify(key, ignore_dicts=True): _byteify(value, ignore_dicts=True)
for key, value in data.iteritems()
}
# if it's anything else, return it in its original form
return data

用法示例:

>>> json_loads_byteified('{"Hello": "World"}')
{'Hello': 'World'}
>>> json_loads_byteified('"I am a top-level string"')
'I am a top-level string'
>>> json_loads_byteified('7')
7
>>> json_loads_byteified('["I am inside a list"]')
['I am inside a list']
>>> json_loads_byteified('[[[[[[[["I am inside a big nest of lists"]]]]]]]]')
[[[[[[[['I am inside a big nest of lists']]]]]]]]
>>> json_loads_byteified('{"foo": "bar", "things": [7, {"qux": "baz", "moo": {"cow": ["milk"]}}]}')
{'things': [7, {'qux': 'baz', 'moo': {'cow': ['milk']}}], 'foo': 'bar'}
>>> json_load_byteified(open('somefile.json'))
{'more json': 'from a file'}

byteify的更多相关文章

  1. 制作Wi-Fi Ducky远程HID攻击设备

    1.介绍WIFI DUCKY 它是一个Wi-Fi控制的BadUSB设备来远程执行Ducky Scripts. 使用充当键盘的USB设备来注入攻击,Hak5 的 USB Rubber Ducky 是这种 ...

  2. Python27中Json对中文的处理

    应用场景如下:从api下载数据,json解析,存入字典,定期保存.重启程序需要加载保存的文本. 问题1:json中都是unicode串,存到文本里都是些\u*** 解决:关闭ensure_ascii开 ...

  3. python json unicode utf-8处理总结

    1.直接输出字典中文 在python中经常遇见直接print dict(字典),或者dict转json,但是没有给特定的参数,然后打印json字符串,输出的中文就成了unicode码的情况,如下: d ...

随机推荐

  1. vue-cli中的check-versions.js配置文件包括semver,chalk,shell插件的解释

    本文介绍vue-cli脚手架build目录中check-versions.js的配置 本文件是用来检测node和npm版本的 直接上代码加注释 // 下面的插件是chalk插件,他的作用是在控制台中输 ...

  2. B树、B-树、B+树、B*树相关

    B树 即二叉搜索树: 1.所有非叶子结点至多拥有两个儿子(Left和Right): 2.所有结点存储一个关键字: 3.非叶子结点的左指针指向小于其关键字的子树,右指针指向大于其关键字的子树: 如: B ...

  3. 2017-2018-2 1723《程序设计与数据结构》实验四 & 实验五 & 课程总结 总结

    作业地址 实验四作业:https://edu.cnblogs.com/campus/besti/CS-IMIS-1723/homework/1943 提交情况如图: 实验五作业:https://edu ...

  4. 语音笔记:MFCC

    一,传统语音识别体系结构 二,MFCC特征提取 MFCC(Mel-frequency cepstral coefficients):梅尔频率倒谱系数.梅尔频率是基于人耳听觉特性提出来的, 它与Hz频率 ...

  5. <构建之法>第三10、11、12章

    第十章 典型用户和场景 问题:如何更准确知道用户的需求是什么,设计出满足用户的软件? 第十一章 软件设计与实现 问题:软件设计过程中,如何管理设计变更? 第十二章 用户体验 问题:在何时开始设计用户体 ...

  6. 第一个Sprint第一天

    第一个Sprint冲刺的第一天 组员:陈建定 陈友沛 林清松 我们小组选的题目是小学四则运算APP 之前我们都没有做过这个程序,不过我们会尽力完成. 第一阶段的冲刺我们主要分析这个APP的所需的功能, ...

  7. 07-java学习-方法重载-idea集成开发工具学习-项目-模块-包

    方法重载的概念? 方法重载的好处? 集成开发工具idea的学习 下载 安装 设置 建项目 导入项目 建模块 导入模块 建包 复制粘贴包 建类 复制粘贴类 运行 调试

  8. FreeMarker boolean Issue

    FreeMarker template error:Can't convert boolean to string automatically, because the "boolean_f ...

  9. liuyan

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...

  10. 自己站点的nginx 配置信息

    user www www; worker_processes auto; error_log /home/wwwlogs/nginx_error.log crit; pid /usr/local/ng ...