Python之Json文件操作
json
适用于现代 C++ 的 JSON。
项目地址:https://gitcode.com/gh_mirrors/js/json

·
Python之Json文件操作
Python对Json文件的操作主要用到的model是:json, 所以首先我们要把json加载进来:
import json
然后我们用到的是json下的 json.load / json.dumps 函数,结合IO的基本文件操作函数 open/write/close实现对json文件的操作。
1. 首先我们需要新建一个json文件:test.json
2. 打开并获取到json下的内容:
file = JsonPath + '/test.json'
fb = open(file,'r')
dicts = json.load(fb)
fb.close()
return dicts
解析:从代码里面可以看到,首先我们需要知道test,json文件的路径: file,然后通过open函数打开json文件,再通过json.load()函数把json文件下的内容赋到dicts。返回的dicts就是json下的所有内容,对单个节点的获取方式是:dicts[‘name‘]。
3. 修改并保存在json文件下:
data['name1'] = data['name1']
data['name2'] = "This is name 2“
file =JsonPath + '/test.json'
fb = open(file,'w')
fb.write(json.dumps(data,indent=2))
fb.close()
解析:这里的data是前面返回的dicts。我们可以看到,我们首先需要获取到json内容,然后通过直接赋值方式修改添加内容,然后结合open/write函数,结合 json.dumps()函数把内容写带json文件下。
这里需要说明一下json.dumps函数的参数:data是修改后的内容,indent是json 中换行后tap文件布局格数。indent=2表示每次换行以后空两格。(indent参数可选)
最后记得对文件的IO操作一定要close。




适用于现代 C++ 的 JSON。
最近提交(Master分支:4 个月前 )
f06604fc
* :page_facing_up: bump the copyright years
Signed-off-by: Niels Lohmann <mail@nlohmann.me>
* :page_facing_up: bump the copyright years
Signed-off-by: Niels Lohmann <mail@nlohmann.me>
* :page_facing_up: bump the copyright years
Signed-off-by: Niels Lohmann <niels.lohmann@gmail.com>
---------
Signed-off-by: Niels Lohmann <mail@nlohmann.me>
Signed-off-by: Niels Lohmann <niels.lohmann@gmail.com> 1 个月前
d23291ba
* add a ci step for Json_Diagnostic_Positions
Signed-off-by: Harinath Nampally <harinath922@gmail.com>
* Update ci.cmake to address review comments
Signed-off-by: Harinath Nampally <harinath922@gmail.com>
* address review comment
Signed-off-by: Harinath Nampally <harinath922@gmail.com>
* fix typo in the comment
Signed-off-by: Harinath Nampally <harinath922@gmail.com>
* fix typos in ci.cmake
Signed-off-by: Harinath Nampally <harinath922@gmail.com>
* invoke the new ci step from ubuntu.yml
Signed-off-by: Harinath Nampally <harinath922@gmail.com>
* issue4561 - use diagnostic positions for exceptions
Signed-off-by: Harinath Nampally <harinath922@gmail.com>
* fix ci_test_documentation check
Signed-off-by: Harinath Nampally <harinath922@gmail.com>
* address review comments
Signed-off-by: Harinath Nampally <harinath922@gmail.com>
* fix ci check failures for unit-diagnostic-postions.cpp
Signed-off-by: Harinath Nampally <harinath922@gmail.com>
* improvements based on review comments
Signed-off-by: Harinath Nampally <harinath922@gmail.com>
* fix const correctness string
Signed-off-by: Harinath Nampally <harinath922@gmail.com>
* further refinements based on reviews
Signed-off-by: Harinath Nampally <harinath922@gmail.com>
* add one more test case for full coverage
Signed-off-by: Harinath Nampally <harinath922@gmail.com>
* ci check fix - add const
Signed-off-by: Harinath Nampally <harinath922@gmail.com>
* add unit tests for json_diagnostic_postions only
Signed-off-by: Harinath Nampally <harinath922@gmail.com>
* fix ci_test_diagnostics
Signed-off-by: Harinath Nampally <harinath922@gmail.com>
* fix ci_test_build_documentation check
Signed-off-by: Harinath Nampally <harinath922@gmail.com>
---------
Signed-off-by: Harinath Nampally <harinath922@gmail.com> 1 个月前
更多推荐
所有评论(0)