numpy.fromstring(string, dtype=float, count=-1, sep='')

Return a new 1-D array initialized from raw binary or text data in string.

Parameters :

string : str

A string containing the data.

dtype : dtype, optional

The data type of the array. For binary input data, the data must be in exactly this format.

count : int, optional

Read this number of dtype elements from the data. If this is negative, then the size will be determined from the length of the data.

sep : str, optional

If provided and not empty, then the data will be interpreted as ASCII text with decimal numbers. This argument is interpreted as the string separating numbers in the data. Extra whitespace between elements is also ignored.

Returns :

arr : array

The constructed array.

Raises :

ValueError :

If the string is not the correct size to satisfy the requested dtype and count.


>>> np.fromstring('\x01\x02', dtype=np.uint8)
array([1, 2], dtype=uint8)
>>> np.fromstring('1 2', dtype=int, sep=' ')
array([1, 2])
>>> np.fromstring('1, 2', dtype=int, sep=',')
array([1, 2])
>>> np.fromstring('\x01\x02\x03\x04\x05', dtype=np.uint8, count=3)
array([1, 2, 3], dtype=uint8)

Invalid inputs:

>>> np.fromstring('\x01\x02\x03\x04\x05', dtype=np.int32)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: string size must be a multiple of element size
>>> np.fromstring('\x01\x02', dtype=np.uint8, count=3)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: string is smaller than requested size

Previous topic


Next topic


This Page