numpy.fromstring

numpy.fromstring

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

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

Parameters:

string : str

A string containing the data.

dtype : data-type, optional

The data type of the array; default: float. 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 isnegative (the default), the count will be determined from thelength of the data.

sep : str, optional

If not provided or, equivalently, the empty string, the data willbe interpreted as binary data; otherwise, as ASCII text withdecimal numbers. Also in this latter case, this argument isinterpreted as the string separating numbers in the data; extrawhitespace between elements is also ignored.

Returns:

arr : ndarray

The constructed array.

Raises:

ValueError

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

See also

frombuffer, fromfile, fromiter

Examples

>>> 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)

Previous topic

numpy.fromiter

Next topic

numpy.loadtxt

你可能感兴趣的:(numpy)