首页 > 其他分享 >numpy 读写文件

numpy 读写文件

时间:2023-02-02 23:33:35浏览次数:41  
标签:文件 None 读写 test npz np array numpy

 

读写bin文件


    ndarray.tofile(fid, sep="", format="%s")
        将数组作为txt或bin(默认)写入文件, 不保存数组形状和元素类型
        fid: 文件对象/文件名
        sep: 分隔符
        format: 格式化字符串

    ndarray.fromfile(file, dtype=float, count=-1, sep=’’, offset=0)
        读取数据时,需要指定元素类型和数组形状
        file: 文件对象/文件名
        dtype: 数据类型. 需要指定对应的元素类型,否则数据错误
        count: 读取的项目数 -1 表示所有
        sep: 分隔符.’'表示文件应被视为二进制文件, 以空格作为分隔符, 至少需要匹配一个空格
        offset: 偏移量.以字节为单位, 默认是0, 仅允许用于二进制文件

import numpy as np
a = np.arange(12).reshape(3, 4)

array([[ 0,  1,  2,  3],
       [ 4,  5,  6,  7],
       [ 8,  9, 10, 11]])
# 保存为bin文件
a.tofile('test.bin')

# 读取bin文件, 不指定类型 数据错误
np.fromfile('test.bin')

array([2.12199579e-314, 6.36598737e-314, 1.06099790e-313, 1.48539705e-313,
       1.90979621e-313, 2.33419537e-313])

# 指定正确的类型 和 形状
b = np.fromfile('test.bin', dtype=np.int32).reshape(3,4)

array([[ 0,  1,  2,  3],
       [ 4,  5,  6,  7],
       [ 8,  9, 10, 11]])

 

 

读写npy/npz文件



npy是NumPy专用的二进制格式, 保存了array的形状和类型信息

npz是包含多个array的压缩格式

    np.save(file,arr,allow_pickle = True,fix_imports = True )
        将一个array保存为npy文件
        file: 文件对象/文件名
        arr: 要保存的array
        allow_pickle: 允许使用Python pickles 保存数组. 默认是True
        fix_imports: 修复导入.默认为True
    np.savez(file,arr,*args,**kwargs )
        如果传入的参数没有关键字,.npz文件中相应的变量名为“arr_0”、“arr_1”等。如果给定关键字参数,.npz文件中相应的变量名将与关键字名匹配。
        file: npz 文件
        args
        kwargs
    np.load(file, mmap_mode=None**,** allow_pickle=False**,** fix_imports=True**,** encoding='ASCII’)
        从npy、npz或pickled文件加载数组或pickled对象。

import numpy as np
a = np.arange(12).reshape(3, 4)
array([[ 0,  1,  2,  3],
       [ 4,  5,  6,  7],
       [ 8,  9, 10, 11]])
# 保存为npy
np.save('test.npy', a)

# 读取npy
c = np.load('test.npy')

# 保存为npz
d = np.arange(9).reshape(3,3)
np.savez('test.npz')

# 读取npz
e = np.load('test.npz')
# 文件名
e.files
['arr_0', 'arr_1']


# 指定文件名
np.savez('test.npz', a=a, d=d)
f = np.load('test.npz')
f.files
['a', 'd']

f['a']
array([[ 0,  1,  2,  3],
       [ 4,  5,  6,  7],
       [ 8,  9, 10, 11]])

f['d']
array([[0, 1, 2],
       [3, 4, 5],
       [6, 7, 8]])

 

 

 

读写txt/csv文件


    np.savetxt(fname,X, fmt=’%.18e’,delimiter=’ ',newline=’\n’, header=’’, footer=’’,comments=’#’,encoding=None)
        将数组保存为文本文件
        fname:文件对象或者文件名
        X:要保存的数组array
        fmt:格式化
        delimiter:分隔符
        newline:换行符
        header:文件头
        footer: 页脚
        comments:注释
        encoding: 编码


    np.loadtxt(fname, dtype=float, comments=’#’, delimiter=None, converters=None, skiprows=0, usecols=None, unpack=False, ndmin=0, encoding=‘bytes’, max_rows=None, *, like=None)

Returns:out  ndarray


        读取文本文件
        fname:文件对象或者文件名 .gz/.bz2会自动解压
        dtype: 数据类型
        comments:注释
        delimiter:分隔符
        converters:转换器. 如果第0列是日期字符. converters={0:datestr2num}
        skiprows:跳过第几行, 默认是0, 读取所有行
        usecols: 读取第几列, 默认为None. 读取所有列
        unpack:解包, True时, 可自动解包x,y,z=loadtxt(file)
        ndmin: 最小维度
        encoding: 编码
        max_rows: 最大行数. 读取skiprows后的最大行数

import numpy as np
a = np.arange(12)
np.savetxt('test.txt', a, fmt='%.4f')

f = np.loadtxt('test.txt')
 

 

REF

链接:https://blog.csdn.net/linzi1994/article/details/116940017

 

标签:文件,None,读写,test,npz,np,array,numpy
From: https://www.cnblogs.com/emanlee/p/17087730.html

相关文章