Skip to content

Instantly share code, notes, and snippets.

@jlumpe
Created July 13, 2016 04:42
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Embed
What would you like to do?
.npy info #numpy
import struct, ast
def npy_info(fh):
"""Get information about a .npy file, without reading in all the data.
See http://docs.scipy.org/doc/numpy/neps/npy-format.html
"""
# Check magic number
if fh.read(6) != b'\x93NUMPY':
raise IOError('Magic number not found, file is not .npy format or is corrupt')
# Get version
major, minor = struct.unpack('bb', fh.read(2))
# Header length
if major == 1:
header_len, = struct.unpack('<h', fh.read(2))
else:
header_len, = struct.unpack('<i', fh.read(4))
# Header
header_str = fh.read(header_len).strip()
header = ast.literal_eval(header_str)
return dict(
version=(major, minor),
header=header,
data_offset=10 + header_len,
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment