Skip to content

numpy.fromfile() byte offset #12834

@danielhogg

Description

@danielhogg

A major use case for the numpy.fromfile() function is in reading from binary file of some kind. Frequently, binary formats are just arrays of data with a fixed-size header. It would be useful to have an extra optional parameter for the fromfile() function which defines a byte offset for when to start reading. This parameter would default to 0, leaving it backwards-compatible with existing code.

Reproducing code example:

import numpy as np

binary_file = "sample_binary.bin"
my_array = np.fromfile(binary_file, dtype=np.uint8, offset=40)

Currently, the above code will not run, as the 'offset' keyword argument is not supported. In this call, the ndarray object 'my_array' would be the result of the binary file being read in, starting with an offset of 40 bytes.

It is certainly possible to work-around the issue currently, but adding direct support for offsets in the function would make for cleaner code.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions