美文网首页
多维数据的存取

多维数据的存取

作者: 夏天才爱睡觉 | 来源:发表于2017-11-06 18:10 被阅读0次

tofile和fromfile

a.tofile(frame, sep='', format='%s')
  • frame : 文件、字符串
  • sep : 数据分割字符串,如果是空串,写入文件为二进制
  • format : 写入数据的格式
In [94]: a=np.arange(100).reshape(5,10,2)

In [95]: a.tofile('b.dat',sep=',',format='%d')

b.dat文件

0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50,51,52,53,54,55,56,57,58,59,60,61,62,63,64,65,66,67,68,69,70,71,72,73,74,75,76,77,78,79,80,81,82,83,84,85,86,87,88,89,90,91,92,93,94,95,96,97,98,99
In [96]: a=np.arange(100).reshape(5,10,2)

In [97]: a.tofile('b.dat',format='%d')

b.dat文件

    �   �   �   �   �   �   �   �          
   �      
   �   �   �   �   �   �   �   �   �   �   �   �   �   �   �   �   �   �       !   "   #   $   %   &   '   (   )   *   +   ,   -   .   /   0   1   2   3   4   5   6   7   8   9   :   ;   <   =   >   ?   @   A   B   C   D   E   F   G   H   I   J   K   L   M   N   O   P   Q   R   S   T   U   V   W   X   Y   Z   [   \   ]   ^   _   `   a   b   c   
np.fromfile(frame, dtype=float, count=‐1, sep='')
  • frame : 文件、字符串
  • dtype : 读取的数据类型
  • count : 读入元素个数,‐1表示读入整个文件
  • sep : 数据分割字符串,如果是空串,写入文件为二进制
In [101]: a=np.arange(100).reshape(5,10,2)

In [102]: a.tofile('b.dat',sep=',',format='%d')

In [103]: c=np.fromfile('b.dat',dtype=np.int,sep=',').reshape(5,10,2)

In [104]: c
Out[104]: 
array([[[ 0,  1],
        [ 2,  3],
        [ 4,  5],
        [ 6,  7],
        [ 8,  9],
        [10, 11],
        [12, 13],
        [14, 15],
        [16, 17],
        [18, 19]],

       [[20, 21],
        [22, 23],
        [24, 25],
        [26, 27],
        [28, 29],
        [30, 31],
        [32, 33],
        [34, 35],
        [36, 37],
        [38, 39]],

       [[40, 41],
        [42, 43],
        [44, 45],
        [46, 47],
        [48, 49],
        [50, 51],
        [52, 53],
        [54, 55],
        [56, 57],
        [58, 59]],

       [[60, 61],
        [62, 63],
        [64, 65],
        [66, 67],
        [68, 69],
        [70, 71],
        [72, 73],
        [74, 75],
        [76, 77],
        [78, 79]],

       [[80, 81],
        [82, 83],
        [84, 85],
        [86, 87],
        [88, 89],
        [90, 91],
        [92, 93],
        [94, 95],
        [96, 97],
        [98, 99]]])

注意:

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

NumPy的便捷文件存取

np.save(fname, array)或np.savez(fname, array)
  • fname : 文件名,以.npy为扩展名,压缩扩展名为.npz
  • array : 数组变量
np.load(fname)
  • fname : 文件名,以.npy为扩展名,压缩扩展名为.npz
In [105]: a=np.arange(100).reshape(5,10,2)

In [106]: np.save('a.npy',a)

In [107]: b=np.load('a.npy')

In [108]: b
Out[108]: 
array([[[ 0,  1],
        [ 2,  3],
        [ 4,  5],
        [ 6,  7],
        [ 8,  9],
        [10, 11],
        [12, 13],
        [14, 15],
        [16, 17],
        [18, 19]],

       [[20, 21],
        [22, 23],
        [24, 25],
        [26, 27],
        [28, 29],
        [30, 31],
        [32, 33],
        [34, 35],
        [36, 37],
        [38, 39]],

       [[40, 41],
        [42, 43],
        [44, 45],
        [46, 47],
        [48, 49],
        [50, 51],
        [52, 53],
        [54, 55],
        [56, 57],
        [58, 59]],

       [[60, 61],
        [62, 63],
        [64, 65],
        [66, 67],
        [68, 69],
        [70, 71],
        [72, 73],
        [74, 75],
        [76, 77],
        [78, 79]],

       [[80, 81],
        [82, 83],
        [84, 85],
        [86, 87],
        [88, 89],
        [90, 91],
        [92, 93],
        [94, 95],
        [96, 97],
        [98, 99]]])

a.npy文件

揘UMPY� F {'descr': '<i4', 'fortran_order': False, 'shape': (5, 10, 2), }      
    �   �   �   �   �   �   �   �          
   �      
   �   �   �   �   �   �   �   �   �   �   �   �   �   �   �   �   �   �       !   "   #   $   %   &   '   (   )   *   +   ,   -   .   /   0   1   2   3   4   5   6   7   8   9   :   ;   <   =   >   ?   @   A   B   C   D   E   F   G   H   I   J   K   L   M   N   O   P   Q   R   S   T   U   V   W   X   Y   Z   [   \   ]   ^   _   `   a   b   c   

相关文章

网友评论

      本文标题:多维数据的存取

      本文链接:https://www.haomeiwen.com/subject/nzglmxtx.html