official website
https://numpy.org/doc/stable/reference/routines.io.html
text file
read text file
illustrate
np.loadtxt()
- File format: plain text file, each row of data can be of different data types.
- Parameter setting: common parameters include
delimiter
specifying the delimiter,dtype
specifying the data type,skiprows
specifying the number of skipped rows, etc. - Applicable scenarios: suitable for reading simple plain text data, such as a set of experimental data.
np.genfromtxt()
- File format: plain text file, can handle missing data and irregular data.
- Parameter settings: common parameters include
delimiter
specifying the delimiter,dtype
specifying the data type,skip_header
specifying the number of skipped header lines,missing_values
specifying missing values, etc. - Applicable scenarios: suitable for reading irregular plain text data, such as data containing missing values or blank rows.
np.recfromtxt()
- File format: plain text file, can handle missing data and irregular data.
- Parameter settings: Common parameters are
np.genfromtxt()
similar to . - Applicable scenarios: suitable for reading structured data with column names, such as experimental data tables.
np.recfromcsv()
- File format: CSV file, each row of data is usually the same data type.
- Parameter settings: common parameters and
delimiter
specified delimiter,dtype
specified data type,skip_header
specified number of skipped header lines, etc. - Applicable scenarios: suitable for reading CSV data with column names, such as experimental data tables.
the difference
np.loadtxt()
: Used to read data from a text file and store it as a NumPy array. By default, this function assumes that the data is numeric, separated by spaces, and has no header row. Additionally, you can specify the data type, skip specific rows or columns, and other options.np.genfromtxt()
:loadtxt()
Similar , but more flexible. It can handle missing values, different delimiters, different data types, and different text file formats (such as CSV, TSV, etc.). Additionally, it can handle data files with header and comment lines.np.recfromtxt()
:genfromtxt()
Similar to , but creates a structured array where each column can have a different data type and each column can be identified by a header.np.recfromcsv()
:recfromtxt()
Similar to , but it only handles the CSV file format and uses commas as delimiters by default.
write to text file
np.savetxt()
- File format: plain text file, each row of data can be of different data types.
- Parameter setting: common parameters include
delimiter
specifying separator,fmt
specifying output format, etc. - Applicable scenarios: suitable for saving a set of experimental data into plain text files.
binary file
read binary file
illustrate
np.load()
- File format: NumPy binary.
- Parameter settings: Common parameters include
allow_pickle
specifying whether to allow loading of pickled objects,mmap_mode
specifying the memory mapping mode, etc. - Applicable scenarios: suitable for loading binary files that save NumPy arrays.
np.fromfile()
- File Format: Any binary file.
- Parameter setting: common parameters include
dtype
specifying the data type,count
specifying the number of elements to be read, etc. - Applicable scenarios: suitable for reading large binary files, such as image data.
the difference
np.load()
: Used to load data from NumPy-specific binaries (.npy, .npz). These filesnp.save()
,np.savez()
created by the or functions, can save NumPy arrays on disk along with their metadata (such as data type, shape, etc.).np.load()
Functions can easily read these files and restore them to raw NumPy arrays.np.fromfile()
: Used to load data from a binary file and store it as a NumPy array. This function assumes that the data in the file is contiguous and encoded according to the specified data type. Therefore, you need to specify the data type and the shape of the array in order to correctly interpret the read data as a NumPy array.
In summary, np.load()
both np.fromfile()
and can be used to load data from files, but np.load()
is specifically for loading NumPy binary files, while np.fromfile()
for loading binary files, you need to manually specify the data type and shape.
Write NumPy binary files
illustrate
np.save()
- File format: NumPy binary.
- Parameter settings: Common parameters include
allow_pickle
specifying whether to allow pickle objects,fix_imports
specifying whether to fix differences between Python 2 and Python 3, etc. - Applicable scenarios: Suitable for saving NumPy arrays into binary files.
np.savez()
- File format: Compressed NumPy binaries.
- Parameter setting: common parameters include
allow_pickle
,fix_imports
and and**arrays
so on . - Applicable scenarios: Suitable for saving multiple NumPy arrays into a compressed binary file.
np.savez_compressed()
- File format: Compressed NumPy binaries.
- Parameter settings: Common parameters are
np.savez()
similar to . - Applicable scenarios: It is suitable for saving multiple NumPy arrays into a highly compressed binary file to save storage space.
the difference
np.save()
: Save a single NumPy array to a binary file (.npy) on disk. This file only contains the raw data of the array and does not contain any metadata (such as data type, shape, etc.). Therefore, when you load this file, you need to know the metadata of the original array in order to interpret the data correctly.np.savez()
: Save multiple NumPy arrays to a compressed binary file (.npz) on disk. This file contains arrays and their metadata. This file can access each array by index, just like a dictionary. The argument to this function is a series of NumPy arrays, each of which needs to be assigned a name so that it can be referenced when loading the file.np.savez_compressed()
:np.savez()
Similar to , but saves with a higher compression ratio, resulting in a smaller file size. This function takes thenp.savez()
same , and the resulting filename also ends with the .npz file extension.