python reads and writes using NumPy files

  • 2021-10-24 23:23:14
  • OfStack

1. Read and write text files using NumPy

In data analysis, it is often necessary to read data from files or write data into files. The commonly used formats for storing files include text files, CSV format files, binary format files and multidimensional data files.

1. Write a 1-or 2-dimensional array to an TXT file or an CSV file. In NumPy, use the savetxt () function to write a 1-or 2-dimensional array to a file with the suffix txt or csv. The function format is:


**numpy.savetxt(fname,array,fmt='%.18e',delimiter=None,newline='\n', header='', footer='', comments='# ', encoding=None)**

Main parameters:
fname: A file, string, or generator, which can be a compressed file of. gz or. bz2
array: Array to file (1-D or 2-D)
fmt: Write file format, such as:% d,%. 2f,%. 18e, default is%. 18e optional
delimiter: Delimiter, usually str optional
header: String to be written at the beginning of the file
footer: The string to be written at the end of the file
comments: The string to be appended to the header and footer strings to mark them as comments.
Default: '#' encoding: Encoding used to encode the output file.


import numpy as np
arr = np.arange(12).reshape(3,4)
#fmt By default %.18e( Floating point number) 
# The delimiter is a space by default, and the written file is saved in the current directory 
np.savetxt('test-1.txt',arr)
#fmt:%d  The elements written to the file are 10 Binary integer , The divider is a comma ",", The write file is saved in the current directory 
np.savetxt('test-2.txt',arr,fmt='%d',delimiter=',')
# In test-3.txt Add comments to the header and tail of the file, and the header  #test-3 , tail  #  Data write comment, the element written to the file is a string 
np.savetxt('test-3.txt',arr,fmt='%s',delimiter=',',header=\
  'test-3',footer=' Test data ',encoding='utf-8')
# In test-4.txt File header plus ##test-4 Notes 
np.savetxt('test-4.txt',arr,fmt='%f',delimiter=',',header=
  'test-4',comments='###')
# Will arr Array is saved as csv Documents 
np.savetxt('test-1.csv',arr,fmt='%d',header='test-1')

2. Read TXT file and CSV format file in NumPy, the function to read TXT file and CSV format file is loadtxt (), function format:

numpy.loadtxt(fname,dtype=type'float' > , comments= '#', delimiter=None, converters=None, skiprows=0, usecols=None, unpack=False, ndmin=0, encoding= 'bytes')
# Parameter description:
fname: The name of the file to be read (relative or absolute address of the file)
dtype: Specifies the data type of the read data
comments: Skip the line at the beginning of the specified parameter in the file (that is, do not read)
delimiter: Specifies the divider to read the data in the file
converters: Preprocessing Read Data
skiprows: Select the number of rows to skip
usecols: Specify columns to read
unpack: Select whether to output data vectors
encoding: Precoding the read file


a = np.loadtxt('test-1.txt') 
# Read a file in the current directory  test-1.txt
print(a) 
[[ 0. 1. 2. 3.]
 [ 4. 5. 6. 7.]
 [ 8. 9. 10. 11.]]

# skiprows: Before skipping 1 Row ,  If you set the skiprows=2,  You skip the first two lines , Set the data type to integer .
a = np.loadtxt('test-1.txt', skiprows=1, dtype=int)
print(a)
[[ 4 5 6 7]
 [ 8 9 10 11]]

# comment,  If the line begins with # The line is skipped 
a = np.loadtxt('test-4.txt', skiprows=2, comments='#',delimiter=',')
b = np.loadtxt('test-4.txt',comments='#',delimiter=',')
print(a,b,sep='\n')
[[ 4. 5. 6. 7.]
 [ 8. 9. 10. 11.]]
[[ 0. 1. 2. 3.]
 [ 4. 5. 6. 7.]
 [ 8. 9. 10. 11.]]

# usecols: Specifies the column to read, and if the read 0,2 Two columns 
aa = np.loadtxt('test-3.txt',dtype=int, skiprows=1,delimiter=',',usecols=(0, 2))
#unpack It means that every 1 Column as 1 Vector output ,  Instead of merging in 1 Get up. 
(a, b) = np.loadtxt('test-2.txt', dtype=int, skiprows=1,
   comments='#', delimiter=',',
   usecols=(0, 2), unpack=True)
print(aa,a, b,sep='\n')
[[ 0 2]
 [ 4 6]
 [ 8 10]]
[4 8]
[ 6 10]
# Read csv Documents 
aa = np.loadtxt('test-1.csv',skiprows=1)
print(aa)
[[ 0. 1. 2. 3.]
 [ 4. 5. 6. 7.]
 [ 8. 9. 10. 11.]]

2. Read and write binary files using NumPy

1. Write a binary format file using the save () or savez () function

The save function saves the array in an uncompressed raw binary format in a file with the. npy extension. Information such as element types and shapes is automatically processed.
The savez function compresses multiple arrays into a file with the extension npz, where each file is an npy file saved by save () with the same name as the array
Format of save () or savez () functions:


numpy.save(file,array)
numpy.savez(file,array)

2. Use the load () function to read a binary format file

Format of load () function: numpy. load (file)


import numpy as np
a = np.arange(12).reshape(3,4)
print(' Original array a:\n',a)
np.save('arr1.npy', a) # Store data as npy, You can omit the extension when saving, and the default .npy
c = np.load('arr1.npy') # Read arr1.npy Data of , You can't omit when reading data  .npy
print(' Data after reading: \n',c)

ar = np.arange(6).reshape(3,2) 

print(' Array before saving :',a,ar,sep='\n')
np.savez('arr2.npz',a,ar) # Majority group storage, default file name .npz
b = np.load('arr2.npz')
print(' Data after reading: ')
print(b['arr_0'],b['arr_1'],sep='\n')

Original array a:
[[ 0 1 2 3]
[ 4 5 6 7]
[ 8 9 10 11]]
Data after reading:
[[ 0 1 2 3]
[ 4 5 6 7]
[ 8 9 10 11]]
Array before saving:
[[ 0 1 2 3]
[ 4 5 6 7]
[ 8 9 10 11]]
[[0 1]
[2 3]
[4 5]]
Data after reading:
[[ 0 1 2 3]
[ 4 5 6 7]
[ 8 9 10 11]]
[[0 1]
[2 3]
[4 5]]


for i in b.items():
 print(i)
('a', array([[ 0, 1, 2, 3],
 [ 4, 5, 6, 7],
 [ 8, 9, 10, 11]]))
('ar', array([[0, 1],
 [2, 3],
 [4, 5]]))

The above is python using NumPy file read-write operation details, more about python using NumPy read-write file information please pay attention to other related articles on this site!


Related articles: