python之numpy文件操作

目录

numpy 中的文件操作总结

CVS文件

CSV (Comma‐Separated Value,逗号分隔值),是一种常见的文件格式,用来存储批量数据

存储:

np.savetxt(frame, array, fmt='%.18e', delimiter=None)
  • frame : 文件、字符串或产生器,可以是.gz或.bz2的压缩文件
  • array : 存入文件的数组
  • fmt : 写入文件的格式,例如:%d %.2f %.18e
  • delimiter : 分割字符串,默认是任何空格
a = np.arange(50).reshape(5,10)
np.savetxt("a.cvs", a, fmt = "%d", delimiter=",")

读取:

np.loadtxt(frame, dtype=np.float, delimiter=None,unpack=False)
  • frame : 文件、字符串或产生器,可以是.gz或.bz2的压缩文件
  • dtype : 数据类型,可选
  • delimiter : 分割字符串,默认是任何空格
  • unpack : 如果True,读入属性将分别写入不同变量
b = np.loadtxt("a.cvs", dtype = np.int, delimiter = ",")

CSV只能有效存储一维和二维数组
np.savetxt(), np.loadtxt()只能有效存取一维和二维数组

多维数据的存取

存储:

a.tofile(frame, sep='', format='%s')
  • frame : 文件、字符串
  • sep : 数据分割字符串,如果是空串,写入文件为二进制
  • format : 写入数据的格式
a = np.array(50).reshape(5,5,2)
a.tofile("b.bat", sep = ",", format = "%d")

读取:

np.fromfile(frame, dtype=np.float, count=‐1, sep='')
  • frame : 文件、字符串
  • dtype : 读取的数据类型
  • count : 读入元素个数,‐1表示读入整个文件
  • sep : 数据分割字符串,如果是空串,写入文件为二进制
c = np.fromfile('b.bat', dtype = np.int, sep = ',')

c
array([0,1,2,3,...,49])

c = np.fromfile('b.bat', dtype = np.int, sep = ',').reshape(5,5,2)

注意:该方法需要读取时知道存入文件时数组的维度和元素类型,
a.tofile()和np.fromfile()需要配合使用 可以通过元数据文件来存储额外信息

numpy 的便捷文件存取

np.save(fname, array) 或 np.savez(fname, array)

  • fname : 文件名,以.npy为扩展名,压缩扩展名为.npz
  • array : 数组变量

np.load(fname)

  • fname : 文件名,以.npy为扩展名,压缩扩展名为.npz
a = np.array(50).reshape(5,5,2)
np.save("a.npy", a)
b = np.load('a.npy')

猜你喜欢

转载自www.cnblogs.com/tzhao/p/9844262.html