NumPy Input and Output: fromstring() function

numpy.fromstring() function

The fromstring() function is used to get a new 1-D array initialized from raw binary or text data in a string.

Syntax:

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

Version: 1.15.0

Parameter:

Name Description Required /
Optional
string A string containing the data.
str
Required
dtype The data type of the array; default: float. For binary input data, the data must be in exactly this format.
data-type
Optional
count Read this number of dtype elements from the data. If this is negative (the default), the count will be determined from the length of the data.
int
Optional
sep If not provided or, equivalently, the empty string, the data will be interpreted as binary data; otherwise, as ASCII text with decimal numbers.
Also in this latter case, this argument is interpreted as the string separating numbers in the data; extra whitespace between elements is also ignored.
str
Optional

Returns: arr : ndarray
The constructed array.

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

NumPy.fromstring() method Example-1:

``````>>> import numpy as np
>>> np.fromstring('2 4', dtype=int, sep=' ')
``````

Output:

```array([2, 4])
```

NumPy.fromstring() method Example-2:

``````>>> import numpy as np
>>> np.fromstring('2, 4', dtype=int, sep=',')
``````

Output:

```array([2, 4])
```

Python - NumPy Code Editor:

Previous: fromregex() function
Next: ndarray.tofile() function

﻿