Pyhton numpy保存和打开.npz文件方法详解

numpy中的.npz文件是对很多文件的压缩封装。这个文件包含的属性就是它里面封装的文件名。其实这个.npz文件的大小并没有被压缩,它仅仅只是封装了许多文件而已。

numpy 使用savez()来将数组保存为.npz格式文件,使用load()来加载.npz格式的文件。

numpy.savez(file, *args, **kwds)
其中:

  1. file:是你将要保存的文件名,数据将会保存到这个文件中。
  2. args: 是你要保存的数组
  3. kwads:是你给数组在.npz文件中安排的名字,如果你不提供这个参数的话,savez()方法会自动将args中的数组名作为默认数组名。

load()方法有一个属性files,通过调用.files可以知道.npz文件中有哪些数组。详细示例如下:

import numpy as np

arr0 = np.array([0,1,2,3])
arr1 = np.array([4,5,6,7,8])

np.savez('hellofile.npz', a = arr0, b = arr1)

datas = np.load('hellofile.npz')
print(datas.files)
print(datas['a'])
print(datas['b'])
['a', 'b']
[0 1 2 3]
[4 5 6 7 8]
发布了25 篇原创文章 · 获赞 3 · 访问量 1万+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章