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

前端 未结 3 1584
青春惊慌失措
青春惊慌失措 2020-11-29 05:10

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):
    letter         


        
3条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-11-29 05:32

    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))
    

提交回复
热议问题