MCPcopy Index your code
hub / github.com/numpy/numpy / fromstring

Function fromstring

numpy/_core/records.py:754–826  ·  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

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

Callers 1

arrayFunction · 0.70

Calls 4

format_parserClass · 0.85
recarrayClass · 0.85
dtypeMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…