mindspore.dataset.text.to_bytes

View Source On Gitee
mindspore.dataset.text.to_bytes(array, encoding='utf8')[source]

Convert NumPy array of str to array of bytes by encoding each element based on charset encoding .

Parameters
  • array (numpy.ndarray) – Array of str type representing strings.

  • encoding (str) – Indicating the charset for encoding. Default: 'utf8'.

Returns

numpy.ndarray, NumPy array of bytes .

Examples

>>> import numpy as np
>>> import mindspore.dataset as ds
>>> import mindspore.dataset.text as text
>>>
>>> data = np.array([["1", "2", "3"]], dtype=np.str_)
>>> dataset = ds.NumpySlicesDataset(data, column_names=["text"])
>>> result = []
>>> for item in dataset.create_dict_iterator(num_epochs=1, output_numpy=True):
...     result.append(text.to_bytes(item["text"]))
>>> print(result)
[array([b'1', b'2', b'3'], dtype='|S1')]