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/