csv文件
CSV (Comma‐Separated Value, 逗號分隔值)
CSV是一種常見的文件格式,用來存儲批量數(shù)據(jù)
np.savetxt(frame, array, fmt='%.18e', delimiter=None)
- frame : 文件乃沙、字符串或產(chǎn)生器,可以是.gz或.bz2的壓縮文件
- array : 存入文件的數(shù)組
- fmt : 寫入文件的格式,例如:%d %.2f %.18e
- delimiter : 分割字符串诸老,默認是任何空格
In [81]: a=np.arange(100).reshape(5,20)
In [82]: np.savetxt('a.csv',a,fmt='%d',delimiter=',')
a.csv文件
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(frame, dtype=np.float, delimiter=None蹄衷, unpack=False)
- frame : 文件、字符串或產(chǎn)生器厘肮,可以是.gz或.bz2的壓縮文件
- dtype : 數(shù)據(jù)類型愧口,可選
- delimiter : 分割字符串,默認是任何空格
- unpack : 如果True类茂,讀入屬性將分別寫入不同變量
In [83]: b=np.loadtxt('a.csv',delimiter=',')
In [84]: b
Out[84]:
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.]])
In [85]: b=np.loadtxt('a.csv',dtype=np.int,delimiter=',')
In [86]: b
Out[86]:
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]])
csv文件的局限性
CSV只能有效存儲一維和二維數(shù)組
np.savetxt() np.loadtxt()只能有效存取一維和二維數(shù)組