python 数组保存到json 文件
json
适用于现代 C++ 的 JSON。
项目地址:https://gitcode.com/gh_mirrors/js/json

·
Json 格式最初是由JavaScript开发的,但随后成了一种常见的格式
使用json.dump()和json.load()
我们来编写一个存储一组数字的简短程序,再编写一个将这些数字读取到内存中的程序,第一个程序使用json.dump()来储存这组数字,第二个程序将使用json.load()
函数json.dump()接受两个实参:要储存的数据以及可以用于存储数据的文件对象。下面是演示
import json
number = [1,2,3,5]
file_name = 'number.json' #通过扩展名指定文件存储的数据为json格式
with open(file_name,'w') as file_object:
json.dump(number,file_object)
我们先导入json模块,再创建一个是数字列表,我们指定存放在number.json里,文件后缀是.json来指出文件存储的数据是json格式,我们再以写入模式打开文件,让json能见数据写入其中使用json.dup()将数据写入,我们没有写输出语句,打开这个文件查看,数据存储的格式与python一样
注意json.dump()方法,传递两个参数 第一个要写入的,第二个要存储的位置
再写一个程序,使用json.load()读取到内存中
with open(filename,'r') as file_object:
contents = json.load(file_object)
print(contents)
2.个人建议用第二种方法:
numpy.save("filename.npy",a)
利用这种方法,保存文件的后缀名字一定会被置为.npy,这种格式最好只用
numpy.load("filename")




适用于现代 C++ 的 JSON。
最近提交(Master分支:5 个月前 )
34665ae6
binary -> binary_t
Signed-off-by: Robert Chisholm <robert.chisholm@sheffield.ac.uk> 22 天前
f3dc4684
Bumps [github/codeql-action](https://github.com/github/codeql-action) from 3.28.9 to 3.28.10.
- [Release notes](https://github.com/github/codeql-action/releases)
- [Changelog](https://github.com/github/codeql-action/blob/main/CHANGELOG.md)
- [Commits](https://github.com/github/codeql-action/compare/9e8d0789d4a0fa9ceb6b1738f7e269594bdd67f0...b56ba49b26e50535fa1e7f7db0f4f7b4bf65d80d)
---
updated-dependencies:
- dependency-name: github/codeql-action
dependency-type: direct:production
update-type: version-update:semver-patch
...
Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> 28 天前
更多推荐
所有评论(0)