Python JSON - json.dump() 函数使用示例

在 Python 语言中,JSON 对应的数据类型是字典。

1. json.dump(person_dict, json_file)

#!/usr/bin/env python3
# -*- coding: utf-8 -*-

# --------------------------------------------------------
# yongqiang cheng
# --------------------------------------------------------

from __future__ import absolute_import
from __future__ import division
from __future__ import print_function

import json
import os
import sys

sys.path.append(os.path.dirname(os.path.abspath(__file__)))
current_directory = os.path.dirname(os.path.abspath(__file__))

print(16 * "++--")
print("current_directory:", current_directory)
print(16 * "++--")

person_dict = {
    'c': 'cheng',
    'a': 'yong',
    'b': 'qiang',
    'd': 'forever',
    'e': 'strong'
}

with open('person.json', 'w') as json_file:
    json.dump(person_dict, json_file)

/usr/bin/python3.5 /home/strong/project_validation/video_data/json_file_yongqiang.py
++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--
current_directory: /home/strong/project_validation/video_data
++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--

Process finished with exit code 0

在这里插入图片描述

2. json.dump(person_dict, json_file, sort_keys=True, indent=4)

To analyze and debug JSON data, we may need to print it in a more readable format. This can be done by passing additional parameters indent and sort_keys to json.dumps() and json.dump() method.

#!/usr/bin/env python3
# -*- coding: utf-8 -*-

# --------------------------------------------------------
# yongqiang cheng
# --------------------------------------------------------

from __future__ import absolute_import
from __future__ import division
from __future__ import print_function

import json
import os
import sys

sys.path.append(os.path.dirname(os.path.abspath(__file__)))
current_directory = os.path.dirname(os.path.abspath(__file__))

print(16 * "++--")
print("current_directory:", current_directory)
print(16 * "++--")

person_dict = {
    'c': 'cheng',
    'a': 'yong',
    'b': 'qiang',
    'd': 'forever',
    'e': 'strong'
}

with open('person.json', 'w') as json_file:
    json.dump(person_dict, json_file, sort_keys=True, indent=4)

/usr/bin/python3.5 /home/strong/project_validation/video_data/json_file_yongqiang.py
++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--
current_directory: /home/strong/project_validation/video_data
++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--

Process finished with exit code 0

In the above program, we have used 4 spaces for indentation. And, the keys are sorted in ascending order.

By the way, the default value of indent is None. And, the default value of sort_keys is False.

indentation [.ɪnden'teɪʃ(ə)n]:n.凹陷,缺口,凹痕,行首缩进

sort_keys=True 指示编码器按照字典排序 (a 到 z) 输出。如果是字典类型的 Python 对象,将关键字按照字典排序。

indent 表示缩进,默认值为 None,无缩进,不换行。indent=0 时,无缩进,有换行。indent>0 时,有缩进,有换行。

在这里插入图片描述

3. json.dump(person_dict, json_file, indent=4)

#!/usr/bin/env python3
# -*- coding: utf-8 -*-

# --------------------------------------------------------
# yongqiang cheng
# --------------------------------------------------------

from __future__ import absolute_import
from __future__ import division
from __future__ import print_function

import json
import os
import sys

sys.path.append(os.path.dirname(os.path.abspath(__file__)))
current_directory = os.path.dirname(os.path.abspath(__file__))

print(16 * "++--")
print("current_directory:", current_directory)
print(16 * "++--")

person_dict = {
    'c': 'cheng',
    'a': 'yong',
    'b': 'qiang',
    'd': 'forever',
    'e': 'strong'
}

with open('person.json', 'w') as json_file:
    json.dump(person_dict, json_file, indent=4)

/usr/bin/python3.5 /home/strong/project_validation/video_data/json_file_yongqiang.py
++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--
current_directory: /home/strong/project_validation/video_data
++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--

Process finished with exit code 0

在这里插入图片描述

References

[1] Yongqiang Cheng, https://yongqiang.blog.csdn.net/

  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Yongqiang Cheng

梦想不是浮躁,而是沉淀和积累。

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值