Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

re.sub erroring with "Expected string or bytes-like object"

I have read multiple posts regarding this error, but I still can't figure it out. When I try to loop through my function:

def fix_Plan(location):     letters_only = re.sub("[^a-zA-Z]",  # Search for all non-letters                           " ",          # Replace all non-letters with spaces                           location)     # Column and row to search          words = letters_only.lower().split()          stops = set(stopwords.words("english"))           meaningful_words = [w for w in words if not w in stops]           return (" ".join(meaningful_words))      col_Plan = fix_Plan(train["Plan"][0])     num_responses = train["Plan"].size     clean_Plan_responses = []  for i in range(0,num_responses):     clean_Plan_responses.append(fix_Plan(train["Plan"][i])) 

Here is the error:

Traceback (most recent call last):   File "C:/Users/xxxxx/PycharmProjects/tronc/tronc2.py", line 48, in <module>     clean_Plan_responses.append(fix_Plan(train["Plan"][i]))   File "C:/Users/xxxxx/PycharmProjects/tronc/tronc2.py", line 22, in fix_Plan     location)  # Column and row to search   File "C:\Users\xxxxx\AppData\Local\Programs\Python\Python36\lib\re.py", line 191, in sub     return _compile(pattern, flags).sub(repl, string, count) TypeError: expected string or bytes-like object 
like image 515
imanexcelnoob Avatar asked May 01 '17 22:05

imanexcelnoob


People also ask

How do you solve expected strings bytes like objects?

The Python "TypeError: expected string or bytes-like object" occurs when we pass an argument of a different type to a method that expects a string argument, e.g. re. sub() . To solve the error, make sure to pass a string argument to the method.

WHAT IS RE sub in Python?

sub() function belongs to the Regular Expressions ( re ) module in Python. It returns a string where all matching occurrences of the specified pattern are replaced by the replace string. To use this function, we need to import the re module first. import re.


1 Answers

As you stated in the comments, some of the values appeared to be floats, not strings. You will need to change it to strings before passing it to re.sub. The simplest way is to change location to str(location) when using re.sub. It wouldn't hurt to do it anyways even if it's already a str.

letters_only = re.sub("[^a-zA-Z]",  # Search for all non-letters                           " ",          # Replace all non-letters with spaces                           str(location)) 
like image 108
Taku Avatar answered Sep 30 '22 17:09

Taku