如何无需完整加载npz文件即可查看所有元素名称?
Great question—dealing with large .npz files can be a real hassle when you just need to check what's inside without loading everything into memory. Luckily, there are two efficient ways to do this:
Method 1: Use Python's Built-in zipfile Module
Since .npz files are essentially ZIP archives containing individual .npy files (each corresponding to an array in the file), you can directly inspect the archive's contents without touching numpy at all. This method reads only the ZIP directory structure, so it's lightning fast even for huge files:
import zipfile # Replace 'your_large_file.npz' with your actual file path with zipfile.ZipFile('your_large_file.npz', 'r') as zip_ref: # Get all file names in the archive archive_files = zip_ref.namelist() # Extract the keys by stripping the .npy extension from each file name npz_keys = [filename[:-4] for filename in archive_files if filename.endswith('.npy')] print("Available keys in the npz file:", npz_keys)
Method 2: Use NumPy's Lazy-Loading NpzFile Object
If you prefer sticking with numpy, the np.load() function returns a NpzFile object that uses lazy loading—it doesn't load any array data until you explicitly access a key. This means you can safely get the list of keys without loading the entire file:
import numpy as np with np.load('your_large_file.npz', allow_pickle=False) as npz_data: # The keys() method only reads the ZIP directory, not the array contents print("Available keys in the npz file:", list(npz_data.keys()))
Quick Notes:
- Both methods work for both uncompressed (
np.savez()) and compressed (np.savez_compressed()) .npz files—no need to decompress anything upfront. - The
allow_pickle=Falseparameter in the numpy method is a safe default to avoid potential security risks from pickled data; useallow_pickle=Trueonly if you fully trust the source of the .npz file.
内容的提问来源于stack exchange,提问作者user1424739

