paddle.compat. to_bytes ( obj, encoding='utf-8', inplace=False ) [source]

All string in PaddlePaddle should be represented as a literal string.

This function will convert object to a bytes with specific encoding. Especially, if the object type is a list or set container, we will iterate all items in the object and convert them to bytes.

In Python3:

Encode the str type object to bytes type with specific encoding

In Python2:

Encode the unicode type object to str type with specific encoding, or we just return the 8-bit string of object

  • obj (unicode|str|bytes|list|set) – The object to be encoded.

  • encoding (str) – The encoding format to encode a string

  • inplace (bool) – If we change the original object or we create a new one


Decoded result of obj


import paddle

data = "paddlepaddle"
data = paddle.compat.to_bytes(data)
# b'paddlepaddle'