Skip to content
Advertisement

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:

JavaScript

Here is the error:

JavaScript

Advertisement

Answer

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.

JavaScript
User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement