MCPcopy Create free account
hub / github.com/numpy/numpy / fromfile

Function fromfile

numpy/core/records.py:853–950  ·  view source on GitHub ↗

Create an array from binary file data Parameters ---------- fd : str or file type If file is a string or a path-like object then that file is opened, else it is assumed to be a file object. The file object must support random access (i.e. it must have tell and se

(fd, dtype=None, shape=None, offset=0, formats=None,
             names=None, titles=None, aligned=False, byteorder=None)

Source from the content-addressed store, hash-verified

851
852@set_module("numpy.rec")
853def fromfile(fd, dtype=None, shape=None, offset=0, formats=None,
854 names=None, titles=None, aligned=False, byteorder=None):
855 """Create an array from binary file data
856
857 Parameters
858 ----------
859 fd : str or file type
860 If file is a string or a path-like object then that file is opened,
861 else it is assumed to be a file object. The file object must
862 support random access (i.e. it must have tell and seek methods).
863 dtype : data-type, optional
864 valid dtype for all arrays
865 shape : int or tuple of ints, optional
866 shape of each array.
867 offset : int, optional
868 Position in the file to start reading from.
869 formats, names, titles, aligned, byteorder :
870 If `dtype` is ``None``, these arguments are passed to
871 `numpy.format_parser` to construct a dtype. See that function for
872 detailed documentation
873
874 Returns
875 -------
876 np.recarray
877 record array consisting of data enclosed in file.
878
879 Examples
880 --------
881 >>> from tempfile import TemporaryFile
882 >>> a = np.empty(10,dtype='f8,i4,a5')
883 >>> a[5] = (0.5,10,'abcde')
884 >>>
885 >>> fd=TemporaryFile()
886 >>> a = a.newbyteorder('<')
887 >>> a.tofile(fd)
888 >>>
889 >>> _ = fd.seek(0)
890 >>> r=np.core.records.fromfile(fd, formats='f8,i4,a5', shape=10,
891 ... byteorder='<')
892 >>> print(r[5])
893 (0.5, 10, 'abcde')
894 >>> r.shape
895 (10,)
896 """
897
898 if dtype is None and formats is None:
899 raise TypeError("fromfile() needs a 'dtype' or 'formats' argument")
900
901 # NumPy 1.19.0, 2020-01-01
902 shape = _deprecate_shape_0_as_None(shape)
903
904 if shape is None:
905 shape = (-1,)
906 elif isinstance(shape, int):
907 shape = (shape,)
908
909 if hasattr(fd, 'readinto'):
910 # GH issue 2504. fd supports io.RawIOBase or io.BufferedIOBase interface.

Callers 1

arrayFunction · 0.70

Calls 9

openFunction · 0.85
get_remaining_sizeFunction · 0.85
format_parserClass · 0.85
recarrayClass · 0.85
seekMethod · 0.80
dtypeMethod · 0.45
prodMethod · 0.45
indexMethod · 0.45

Tested by

no test coverage detected