使用json.dump()与json.dumps()避免一行显示!
json
适用于现代 C++ 的 JSON。
项目地址:https://gitcode.com/gh_mirrors/js/json
免费下载资源
·
json.dump() 与 json.dumps()使用方法如下:
# -*- coding:utf-8 -*-
# ! ./usr/bin/env python
import json
jsonpath = 'test.json'
data = {
"carDark": {
"name": "CarDark",
"image_files": [
"0001.jpg",
"0002.jpg",
"0003.jpg",
"0004.jpg",
"0005.jpg",
],
"init_rect": [
73,
126,
29,
23
],
}
}
# one line show
data1 = json.dumps(data)
# multi lines show as dict format
data2 = json.dumps(data, indent=2) # indent 表示缩进
# write to json file , one line show
with open(jsonpath, 'w') as f:
json.dump(data, f)
# write to json file , multi lines show as dict format
with open(jsonpath, 'w') as f:
json.dump(data, f, indent=2)
print(data1)
print(data2)
输出:
第一个print输出:
{"carDark": {"image_files": ["0001.jpg", "0002.jpg", "0003.jpg", "0004.jpg", "0005.jpg"], "init_rect": [73, 126, 29, 23], "name": "CarDark"}}
第二个print输出:
{
"carDark": {
"image_files": [
"0001.jpg",
"0002.jpg",
"0003.jpg",
"0004.jpg",
"0005.jpg"
],
"init_rect": [
73,
126,
29,
23
],
"name": "CarDark"
}
}
GitHub 加速计划 / js / json
41.72 K
6.61 K
下载
适用于现代 C++ 的 JSON。
最近提交(Master分支:1 个月前 )
960b763e
3 个月前
8c391e04
6 个月前
更多推荐
已为社区贡献6条内容
所有评论(0)