You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何无需完整加载npz文件即可查看所有元素名称?

How to View .npz File Keys Without Fully Loading the File

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=False parameter in the numpy method is a safe default to avoid potential security risks from pickled data; use allow_pickle=True only if you fully trust the source of the .npz file.

内容的提问来源于stack exchange,提问作者user1424739

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.20 08:59:14