In this article I am going to show you how to fix “TypeError: can’t use a string pattern on a bytes-like object in re.findall()” error.
In my case, this error was caused by the following line of code:
text = text.encode('ascii','ignore')
keywords = re.findall(r'[a-zA-Z]\w+',text)
This code firstly encodes the string into bytes and subsequently tries to apply regular expression to it. The second line is trying to match a string pattern on a byte-like object, which is the
In order to fix this error, we need to decode what we have encoded. Just change your code like this:
text = text.encode('ascii','ignore')
text=text.decode('utf-8')
keywords = re.findall(r'[a-zA-Z]\w+',text)
Problem solved!