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)
| 836 | |
| 837 | @set_module("numpy.rec") |
| 838 | def fromfile(fd, dtype=None, shape=None, offset=0, formats=None, |
| 839 | names=None, titles=None, aligned=False, byteorder=None): |
| 840 | """Create an array from binary file data |
| 841 | |
| 842 | Parameters |
| 843 | ---------- |
| 844 | fd : str or file type |
| 845 | If file is a string or a path-like object then that file is opened, |
| 846 | else it is assumed to be a file object. The file object must |
| 847 | support random access (i.e. it must have tell and seek methods). |
| 848 | dtype : data-type, optional |
| 849 | valid dtype for all arrays |
| 850 | shape : int or tuple of ints, optional |
| 851 | shape of each array. |
| 852 | offset : int, optional |
| 853 | Position in the file to start reading from. |
| 854 | formats, names, titles, aligned, byteorder : |
| 855 | If `dtype` is ``None``, these arguments are passed to |
| 856 | `numpy.format_parser` to construct a dtype. See that function for |
| 857 | detailed documentation |
| 858 | |
| 859 | Returns |
| 860 | ------- |
| 861 | np.recarray |
| 862 | record array consisting of data enclosed in file. |
| 863 | |
| 864 | Examples |
| 865 | -------- |
| 866 | >>> from tempfile import TemporaryFile |
| 867 | >>> a = np.empty(10,dtype='f8,i4,S5') |
| 868 | >>> a[5] = (0.5,10,'abcde') |
| 869 | >>> |
| 870 | >>> fd=TemporaryFile() |
| 871 | >>> a = a.view(a.dtype.newbyteorder('<')) |
| 872 | >>> a.tofile(fd) |
| 873 | >>> |
| 874 | >>> _ = fd.seek(0) |
| 875 | >>> r=np.rec.fromfile(fd, formats='f8,i4,S5', shape=10, |
| 876 | ... byteorder='<') |
| 877 | >>> print(r[5]) |
| 878 | (0.5, 10, b'abcde') |
| 879 | >>> r.shape |
| 880 | (10,) |
| 881 | """ |
| 882 | |
| 883 | if dtype is None and formats is None: |
| 884 | raise TypeError("fromfile() needs a 'dtype' or 'formats' argument") |
| 885 | |
| 886 | # NumPy 1.19.0, 2020-01-01 |
| 887 | shape = _deprecate_shape_0_as_None(shape) |
| 888 | |
| 889 | if shape is None: |
| 890 | shape = (-1,) |
| 891 | elif isinstance(shape, int): |
| 892 | shape = (shape,) |
| 893 | |
| 894 | if hasattr(fd, 'readinto'): |
| 895 | # GH issue 2504. fd supports io.RawIOBase or io.BufferedIOBase |
no test coverage detected
searching dependent graphs…