numpy.fromstring#
- numpy.fromstring(string,dtype=float,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: float. For binary input data,the data must be in exactly this format. Most builtin numeric types aresupported and extension types may be supported.
- countint, optional
Read this number of
dtypeelements from the data. If this isnegative (the default), the count will be determined from thelength of the data.- sepstr, optional
The string separating numbers in the data; extra whitespace betweenelements is also ignored.
Deprecated since version 1.14:Passing
sep='', the default, is deprecated since it willtrigger the deprecated binary mode of this function. This modeinterpretsstringas binary bytes, rather than ASCII text withdecimal numbers, an operation which is better speltfrombuffer(string,dtype,count). Ifstringcontains unicodetext, the binary mode offromstringwill first encode it intobytes using utf-8, which will not produce sane results.- likearray_like, optional
Reference object to allow the creation of arrays which are notNumPy arrays. If an array-like passed in as
likesupportsthe__array_function__protocol, the result will be definedby it. In this case, it ensures the creation of an array objectcompatible with that passed in via this argument.New in version 1.20.0.
- Returns:
- arrndarray
The constructed array.
- Raises:
- ValueError
If the string is not the correct size to satisfy the requested
dtypeandcount.
See also
Examples
>>>importnumpyasnp>>>np.fromstring('1 2',dtype=int,sep=' ')array([1, 2])>>>np.fromstring('1, 2',dtype=int,sep=',')array([1, 2])