Solving TypeError: can’t use a string pattern on a bytes-like object in re.findall() in Python

TypeError: can’t use a string pattern on a bytes-like object in re.findall() is an error which occurs in Python with re.findall() when you are using a string regex.

In this article I am going to show you why this error is happening and how you can solve this error. I am also going to share with you other possible solutions which have worked for many developers who suffered from this error.

Describing TypeError: can’t use a string pattern on a bytes-like object in re.findall()

The problem happens when we use regex, which is a string on html which is bytes.
Python throws an error when you try to use a string regex on the encoded bytes.

The error message should look like the error in the example bellow.

                                                                       #
Traceback (most recent call last):
    title = re.findall(pattern, html)
  File "C:\Python33\lib\live.py", line 211, in findall
    return _compile(pattern, flags).findall(string)

TypeError: can't use a string pattern on a bytes-like object
                                                                       #

Bellow I will present multiple solutions some have worked for me and others have worked for other developers.

Solution 1 : decode the bytes

First, we should understand that Python does not know how the bytes are encoded, so we need to decode them before using them.

                                                                       #
html = html.decode('ISO-8859-1') 
                                                                       #
                                                                       #
var = re.findall(pattern, html) 
                                                                       #

Now you can use the var in your code and the error should be gone.

Solution 2 : convert into a string

Another solution to the issue is to convert the html into a string using .decode and utf-8.

Just like in the example bellow.

                                                                       #
response.read().decode('utf-8')
                                                                       #

If this article has been useful for your particular case, consider donating to our Kofi account, there is a red button at the top of this page.

Summing-up

I hope this article helped you solve the error, If not, I hope the solutions presented here guided you at least in the right direction. Keep coding and cheers, see you in another article.

If you want to learn more about Python, please check out the Python Documentation : https://docs.python.org/3/