Created
July 13, 2016 04:42
-
-
Save jlumpe/6fa7c77b44a727f40e809066a2446014 to your computer and use it in GitHub Desktop.
.npy info #numpy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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