495 words
2 minutes
VII. NumPy Input&Output

VII. NumPy Input / Output (输入输出)#

NumPy supports two file formats: text files (文本文件) like CSV for human-readable data, and binary files (二进制文件) like .npy / .npz for fast, compact storage. Use text for sharing; use binary for speed.

1. savetxt() / loadtxt() — Text Files (文本文件)#

Core idea: Save/load arrays as human-readable text (CSV, TSV, etc.).

import numpy as np
a = np.array([[1.0, 2.0, 3.0],
[4.0, 5.0, 6.0]])
# Save to CSV (保存为CSV)
np.savetxt('data.csv', a, delimiter=',', fmt='%.2f',
header='col1,col2,col3', comments='')
# Load from CSV (从CSV加载)
b = np.loadtxt('data.csv', delimiter=',', skiprows=1)
print(b)
# [[1. 2. 3.]
# [4. 5. 6.]]
Note: loadtxt is for numeric data only. For mixed types (strings + numbers), use np.genfromtxt() or pandas.read_csv().

2. save() / load() — Binary .npy Format (二进制格式)#

Core idea: Save a single array to a .npy binary file — preserves dtype, shape, and is much faster than text.

a = np.array([1, 2, 3, 4, 5])
np.save('my_array.npy', a) # auto-adds .npy extension
b = np.load('my_array.npy')
print(b) # [1 2 3 4 5]
FormatExtensionSpeedHuman-readable?
Text.csv, .txtSlow✅ Yes
Binary.npyFast❌ No

3. savez() / load() — Multiple Arrays (多数组存储)#

Core idea: Save multiple arrays into a single .npz file (a zip of .npy files).

a = np.array([1, 2, 3])
b = np.array([[4, 5], [6, 7]])
np.savez('multi.npz', x=a, y=b) # uncompressed
np.savez_compressed('multi.npz', x=a, y=b) # compressed (smaller)
data = np.load('multi.npz')
print(data['x']) # [1 2 3]
print(data['y']) # [[4 5] [6 7]]

4. tofile() / fromfile() — Raw Binary (原始二进制)#

Core idea: Write raw bytes to disk — no metadata (shape/dtype) is saved. You must know the dtype and shape to reload correctly.

a = np.array([1, 2, 3, 4], dtype=np.int32)
a.tofile('raw.bin')
b = np.fromfile('raw.bin', dtype=np.int32)
print(b) # [1 2 3 4] — shape info is LOST, always 1-D
Note: tofile/fromfile do NOT save dtype or shape. Always use save/load (.npy) unless you need raw binary for interoperability with C/Fortran code.

5. genfromtxt() — Robust Text Loading (鲁棒文本加载)#

Core idea: Like loadtxt but handles missing values (缺失值) and mixed data types.

# CSV with missing values
# 1,2,NaN
# 4,,6
data = np.genfromtxt('messy.csv', delimiter=',',
filling_values=0) # replace NaN with 0

6. Quick Comparison Table#

FunctionFormatPreserves dtype/shape?Use case
savetxt / loadtxtText (CSV)Share data
save / load.npy binaryFast single array
savez / load.npz binaryMultiple arrays
tofile / fromfileRaw binaryC interop
genfromtxtTextMissing values
💡 One-line Takeaway
Use .npy / .npz for fast internal storage, savetxt for sharing CSVs, and genfromtxt when data has missing values.
VII. NumPy Input&Output
https://lxy-alexander.github.io/blog/posts/numpy/api/07numpy-inputoutput/
Author
Alexander Lee
Published at
2026-03-12
License
CC BY-NC-SA 4.0