Python Numpy数组的读入、存储操作

从文件中加载ndarray数组

  • 从文本文件中加载ndarray数组 np.loadtxt

    >>> np.loadtxt(textfile) # textfile是文本文件
  • .npy或者.npz文件中加载ndarray数组np.load
    如果是.npy结尾的文件,则返回单个ndarray数组
    如果是.npz结尾的文件,则返回一个字典类型对象,{filename: array}

    >>> np.load(textfile) # textfile是.npz或.npy结尾的二进制文件

将ndarray数组存入文件

  • 将单个ndarray数组存入一个二进制文件中np.save

    >>> x = np.arange(10) 
    >>> np.save(outfile, x) # outfile必须以.npy结尾
  • 将多个ndarray数组存入一个二进制文件中 np.savez

    >>> x = np.arange(10) 
    >>> y = np.sin(x)
    >>> np.savez(outfile, x,y) # outfile必须以.npz结尾
  • 将ndarray数组存入文本文件中 np.savetxt

    >>> x = np.arange(10) 
    >>> y = np.sin(x)
    >>> np.savetext(outfile, x, delimiter=',') # outfile为文本文件
    >>> np.savetext(outfile, (x,y))
    >>> np.savetext(outfile, x, fmt='%1.4e')

猜你喜欢

转载自www.cnblogs.com/xiashaopengGo/p/9228623.html