numpy
中csv文件的存儲和讀取CSV文件:(Comma‐Separated Value, 逗號分隔值)html
np.savetxt(frame,array,fmt='%.18e',delimiter=None,newline='\n', header='', footer='', comments='# ', encoding=None)
python
其餘參數看文檔。數組
import numpy as np a = np.arange(100).reshape((5,20)) np.savetxt('a.csv',a,fmt = '%d',delimiter=',') b = np.loadtxt('a.csv',delimiter=',') b 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.]])
np.loadtxt(fname, dtype=<type 'float'>, comments='#', delimiter=None, converters=None, skiprows=0, usecols=None, unpack=False, ndmin=0, encoding='bytes')
.net
其餘看文檔。code
注意:usecols,若是選取前三列,應該是usecols=(0,1,2)
,若是隻選取第三列,應該是usecols=(2,)
。可是,我嘗試了一下,不輸入逗號也能夠,usecols=(2)
。orm
b = np.loadtxt('a.csv',dtype = np.int,delimiter=',',usecols=(0,1,2)) b array([[ 0, 1, 2], [20, 21, 22], [40, 41, 42], [60, 61, 62], [80, 81, 82]]) b = np.loadtxt('a.csv',dtype = np.int,delimiter=',',usecols=(2,)) b array([ 2, 22, 42, 62, 82]) b = np.loadtxt('a.csv',dtype = np.int,delimiter=',',usecols=(2)) b array([ 2, 22, 42, 62, 82])
須要注意的是CSV文件只能有效存儲一維和二維數組 。htm
np.savetxt() np.loadtxt()只能有效存取一維和二維數組。blog
a.tofile(frame, sep='', format='%s')
ip
import numpy as np a = np.arange(100).reshape((5,10,2)) a Out[3]: 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('b.dat',sep=',',format='%d')
np.fromfile(frame, dtype=float, count=‐1, sep='')
ci
c = np.fromfile('b.dat',dtype=np.int,sep=',') c Out[6]: 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])
能夠發現,讀取到的數據維度信息丟失了。所以,咱們須要將維度信息告訴c。
c = np.fromfile('b.dat',dtype=np.int,sep=',').reshape((5,10,2)) c Out[14]: 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 = np.arange(100).reshape((5,10,2)) a Out[18]: 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('b.dat',format='%d') c = np.fromfile('b.dat',dtype=np.int).reshape((5,10,2)) c Out[21]: 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]]])
須要注意
np.save(fname, array) 或 np.savez(fname, array)
np.load(fname)
a = np.arange(100).reshape(5, 10, 2) np.save('a.npy',a) b = np.load('a.npy) b = np.load('a.npy') b Out[26]: 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]]])
這裏的存儲,實際上也是二進制文件,若是隻在python中進行操做,這種方法很方便,若是須要與其餘程序進行交互,則須要視狀況存儲爲CSV文件等。