At the heart of NumPy is the ndarray
(n - dimensional array) object. When reading data into NumPy, the data is typically stored in an ndarray
. An ndarray
is a homogeneous, fixed - size, multidimensional container of items of the same type. For example, an array can represent a matrix, a vector, or a higher - dimensional tensor.
NumPy supports a wide range of data types, such as integers (int8
, int16
, etc.), floating - point numbers (float32
, float64
), and more. When reading and writing data, it is important to ensure that the data types are compatible between the source, the NumPy array, and the destination.
The numpy.loadtxt()
function is commonly used to read text files. It assumes that the file contains tabular data, where each line represents a row and the values are separated by a delimiter (usually a space or a comma).
import numpy as np
# Read a text file with space - separated values
data = np.loadtxt('data.txt')
print(data)
# Read a CSV file
data_csv = np.loadtxt('data.csv', delimiter=',')
print(data_csv)
In the above code, the first loadtxt
call reads a text file with space - separated values. The second call reads a CSV (Comma - Separated Values) file by specifying the delimiter as a comma.
To read binary files, we can use the numpy.fromfile()
function. This function reads data from a binary file into a one - dimensional array.
import numpy as np
# Read a binary file
with open('data.bin', 'rb') as f:
data = np.fromfile(f, dtype=np.float32)
print(data)
In this example, we open the binary file in binary read mode ('rb'
) and then use fromfile
to read the data as 32 - bit floating - point numbers.
The numpy.savetxt()
function is used to write data to a text file.
import numpy as np
# Create a sample array
data = np.array([[1, 2, 3], [4, 5, 6]])
# Write the array to a text file
np.savetxt('output.txt', data)
# Write the array to a CSV file
np.savetxt('output.csv', data, delimiter=',')
In this code, the first savetxt
call writes the array to a text file with space - separated values. The second call writes the array to a CSV file by specifying the delimiter as a comma.
The numpy.ndarray.tofile()
method can be used to write a NumPy array to a binary file.
import numpy as np
# Create a sample array
data = np.array([1.0, 2.0, 3.0], dtype=np.float32)
# Write the array to a binary file
with open('output.bin', 'wb') as f:
data.tofile(f)
In this example, we open the binary file in binary write mode ('wb'
) and then use the tofile
method to write the array to the file.
loadtxt
assumes UTF - 8 encoding.loadtxt
may raise an error if the file contains missing values. Special handling is required in such cases.Reading and writing data using NumPy is a crucial skill for anyone working with numerical data in Python. NumPy provides a set of powerful functions and methods to handle different data formats efficiently. By understanding the core concepts, typical usage scenarios, common pitfalls, and best practices, you can effectively read and write data in your scientific and data - driven applications.