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

Function fromstring

numpy/core/records.py:769–841  ·  view source on GitHub ↗

r"""Create a record array from binary data Note that despite the name of this function it does not accept `str` instances. Parameters ---------- datastring : bytes-like Buffer of binary data dtype : data-type, optional Valid dtype for all arrays shape :

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

Source from the content-addressed store, hash-verified

767
768@set_module("numpy.rec")
769def fromstring(datastring, dtype=None, shape=None, offset=0, formats=None,
770 names=None, titles=None, aligned=False, byteorder=None):
771 r"""Create a record array from binary data
772
773 Note that despite the name of this function it does not accept `str`
774 instances.
775
776 Parameters
777 ----------
778 datastring : bytes-like
779 Buffer of binary data
780 dtype : data-type, optional
781 Valid dtype for all arrays
782 shape : int or tuple of ints, optional
783 Shape of each array.
784 offset : int, optional
785 Position in the buffer to start reading from.
786 formats, names, titles, aligned, byteorder :
787 If `dtype` is ``None``, these arguments are passed to
788 `numpy.format_parser` to construct a dtype. See that function for
789 detailed documentation.
790
791
792 Returns
793 -------
794 np.recarray
795 Record array view into the data in datastring. This will be readonly
796 if `datastring` is readonly.
797
798 See Also
799 --------
800 numpy.frombuffer
801
802 Examples
803 --------
804 >>> a = b'\x01\x02\x03abc'
805 >>> np.core.records.fromstring(a, dtype='u1,u1,u1,S3')
806 rec.array([(1, 2, 3, b'abc')],
807 dtype=[('f0', 'u1'), ('f1', 'u1'), ('f2', 'u1'), ('f3', 'S3')])
808
809 >>> grades_dtype = [('Name', (np.str_, 10)), ('Marks', np.float64),
810 ... ('GradeLevel', np.int32)]
811 >>> grades_array = np.array([('Sam', 33.3, 3), ('Mike', 44.4, 5),
812 ... ('Aadi', 66.6, 6)], dtype=grades_dtype)
813 >>> np.core.records.fromstring(grades_array.tobytes(), dtype=grades_dtype)
814 rec.array([('Sam', 33.3, 3), ('Mike', 44.4, 5), ('Aadi', 66.6, 6)],
815 dtype=[('Name', '<U10'), ('Marks', '<f8'), ('GradeLevel', '<i4')])
816
817 >>> s = '\x01\x02\x03abc'
818 >>> np.core.records.fromstring(s, dtype='u1,u1,u1,S3')
819 Traceback (most recent call last)
820 ...
821 TypeError: a bytes-like object is required, not 'str'
822 """
823
824 if dtype is None and formats is None:
825 raise TypeError("fromstring() needs a 'dtype' or 'formats' argument")
826

Callers 1

arrayFunction · 0.70

Calls 4

format_parserClass · 0.85
recarrayClass · 0.85
dtypeMethod · 0.45

Tested by

no test coverage detected