Wu Yuxiong - natural born Numpy library study notes: NumPy IO

Numpy can read and write text or binary data on the disk. 
NumPy introduces a simple file format ndarray objects: npy. 
npy file required for rebuilding ndarray storing data, graphics, dtype and other information. 
IO commonly used functions are: 
the Load () and save () function are the two main functions to read and write files array of data, by default, arrays are uncompressed raw binary format stored in the file extension .npy in. 
savze () function is used to write an array of a plurality of files, by default, it is an array of uncompressed raw binary format stored in the file extension .npz. 
loadtxt () and savetxt () function handles normal text file (.txt, etc.)

Import numpy NP AS 
 
A = np.array ([1,2,3,4,5 ]) 
 
# saved to a file outfile.npy 
np.save ( ' outfile.npy ' , A) 
 
# saved to a file outfile2.npy If no extension at the end of the file path .npy, this extension is automatically added 
np.save ( ' outfile2 ' , A)
import numpy as np 
 
b = np.load('outfile.npy')  
print (b)

Import numpy NP AS 
 
A = np.array ([[l, 2,3], [4,5,6 ]]) 
B = np.arange (0, 1.0, 0.1 ) 
C = np.sin (B)
 # C We used the keyword parameters sin_array 
np.savez ( " runoob.npz " , a, B, sin_array = C) 
R & lt = np.load ( " runoob.npz " )  
 Print (r.files) # Check each array name 
Print (R & lt [ " arr_0 " ]) # array A 
Print (R & lt [ " arr_1 " ]) # array b
Print (R & lt [ " sin_array " ]) # array c

import numpy as np 
 
a = np.array([1,2,3,4,5]) 
np.savetxt('out.txt',a) 
b = np.loadtxt('out.txt')  
 
print(b)
Import numpy NP AS 
 
 
A = np.arange (0,10,0.5) .reshape (. 4, -1 ) 
np.savetxt ( " out.txt " , A, FMT = " % D " , DELIMITER = " , " ) # to save an integer, separated by commas 
B = np.loadtxt ( " out.txt " , DELIMITER = " , " ) # when the load should be specified as a comma-separated 
Print (B)

Guess you like

Origin www.cnblogs.com/tszr/p/12230570.html