## Typeerror: a bytes-like object is required, not ‘str’

Last Updated On Thursday 13th Jan 2022

## TypeError: a bytes-like object is required, not ‘str’

• TypeError: a bytes-like object is required, not 'str' error, we can reproduce it like this
	a = b'123|456'

a.split('|')
TypeError Traceback (most recent call last)

a.split('|')

TypeError: a bytes-like object is required, not 'str'


• The reason for this error is that in Python 3, strings are Unicode

### A Bytes-like object is required, not ‘str’

This error comes up quite often when you are reading text from a file.

To fix the error, the types used by the split() operation should match. The solution is converting to a byte object.Add the prefix, or bytes conversion, to the condition.

Using decode

Python string method decode() decodes the string using the codec registered for encoding.

Syntax

	str.decode(encoding='UTF-8',errors='strict')



Parameters

• encoding − This is the encodings to be used.
• errors − This may be given to set a different error handling scheme.
	a = b'123|456'

a.decode('utf-8').split('|')

# ['123', '456']



The abnormal error is caused by setting the decode() second parameter error sof the method to the strict (strict) form, because the default is this parameter, just change it ignore to.

This is the error raises when you treat an object as a string instead of as a series of bytes.