前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Python 中 的 json 模块

Python 中 的 json 模块

作者头像
py3study
发布2020-01-07 19:45:44
1.1K0
发布2020-01-07 19:45:44
举报
文章被收录于专栏:python3python3

python 中的json 模板主要的两个功能:序列化和反序列化 序列化: encoding   将python 数据 编码成json 字符串 对应的函数有 dump 和 dumps 反序列化: decoding  将json 字符串 解码成 python 数据 对应的函数有 load 和 loads

json 序列化 dumps 实例:

Base example

代码语言:javascript
复制
>>> import json
>>> data=['foo', {'bar': ('baz', None, 1.0, 2)}]
>>> print data
['foo', {'bar': ('baz', None, 1.0, 2)}]
>>> json_data=json.dumps(data)
>>> print json_data
["foo", {"bar": ["baz", null, 1.0, 2]}]
>>>

Compact encoding(压缩编码)

代码语言:javascript
复制
>>> import json
>>> data = [1,2,3,{'4': 5, '6': 7}]
>>> print data
[1, 2, 3, {'4': 5, '6': 7}]
>>> data_json = json.dumps(data)
>>> print data_json
[1, 2, 3, {"4": 5, "6": 7}]
>>> data_json2 = json.dumps(data,sort_keys=True)
>>> print data_json2
[1, 2, 3, {"4": 5, "6": 7}]
>>> data_json2 = json.dumps(data,sort_keys=True,separators=(',',':'))
>>> print data_json2
[1,2,3,{"4":5,"6":7}]

参数 separators 将 , 和 : 后门的空格剔除掉了。 separators 的值必须是一个 tuple 帮助中的英文注释: If specified, separators should be a (item_separator, key_separator) tuple. 

The default is (', ', ': ').  To get the most compact JSON representation you should specify (',', ':') to eliminate whitespace.

Pretty printing(一种格式化输出)

代码语言:javascript
复制
>>> data_json3 = json.dumps(data,sort_keys=True,indent=4,separators=(',',':'))
>>> print data_json3
[
    1,
    2,
    3,
    {
        "4":5,
        "6":7
    }
]

indent 会让每个键值对显示的时候,以缩进几个字符对齐。以方便查看 帮助中的英文注释: If indent is a non-negative integer, then JSON array elements and object members will be pretty-printed with that indent level.  An indent level of 0 will only insert newlines.None is the most compact representation.  Since the default item separator is ', ',  the output might include trailing whitespace when indent is specified.  You can use separators=(',', ': ') to avoid this.

josn 反序列化 loads 实例:

代码语言:javascript
复制
>>> obj = [u'foo', {u'bar': [u'baz', None, 1.0, 2]}]
>>> str_json = '["foo", {"bar":["baz", null, 1.0, 2]}]'
>>> obj2 = json.loads(str_json)
>>> print obj2
[u'foo', {u'bar': [u'baz', None, 1.0, 2]}]
>>> obj2 == obj
True

大数据处理: 以上不论时序列化的dumps 和 反序列化的loads 。所针对的数据都是一个json 字符串 或者时 一个python 的数据结构。那么当遇到了大量的json数据(如一个json 的配置文件) 或者 将一个python 的数据结构导出成一个json 的配置文件。

代码语言:javascript
复制
#! /usr/bin/env python
# _*_ encoding: utf-8 _*_

import json

# dump example
data = [{'lang':('python','java'),'school':"beijing"},"God"]
f = open('test.json','w+')
json.dump(data,f)
f.flush()
f.close()

# load example
fd = file("test.json")
js = json.load(fd)
print js

奇淫巧计:

python 的 json 结合 shell 输出

代码语言:javascript
复制
$ echo '{"json":"obj"}' | python -m json.tool
  {
       "json": "obj"
  }
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2019-09-07 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
相关产品与服务
文件存储
文件存储(Cloud File Storage,CFS)为您提供安全可靠、可扩展的共享文件存储服务。文件存储可与腾讯云服务器、容器服务、批量计算等服务搭配使用,为多个计算节点提供容量和性能可弹性扩展的高性能共享存储。腾讯云文件存储的管理界面简单、易使用,可实现对现有应用的无缝集成;按实际用量付费,为您节约成本,简化 IT 运维工作。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档