numpy.fromstring#
- numpy.fromstring(string, dtype=np.float64, count=-1, *, sep, like=None)#
A new 1-D array initialized from text data in a string.
- Parameters:
- stringstr
A string containing the data.
- dtypedata-type, optional
The data type of the array; default:
numpy.float64. Most builtin numeric types are supported and extension types may be supported.- countint, optional
Read this number of
dtypeelements from the data. If this is negative (the default), the count will be determined from the length of the data.- sepstr
The string separating numbers in the data; extra whitespace between elements is also ignored. This cannot be an empty string.
Changed in version 2.3.0: Binary mode (
sep=''or omitted), which interpretedstringas raw bytes, was removed. Usefrombufferinstead to read raw bytes.- likearray_like, optional
Reference object to allow the creation of arrays which are not NumPy arrays. If an array-like passed in as
likesupports the__array_function__protocol, the result will be defined by it. In this case, it ensures the creation of an array object compatible with that passed in via this argument.Added in version 1.20.0.
- Returns:
- arrndarray
The constructed array.
- Raises:
- ValueError
If sep is omitted or is empty.
See also
Examples
>>> import numpy as np >>> np.fromstring('1 2', dtype=np.int_, sep=' ') array([1, 2]) >>> np.fromstring('1, 2', dtype=np.int_, sep=',') array([1, 2])