逛segmentfault時碰到這個問題,發現早就在stackoverflow上被解決了。
報錯:Expected string or bytes-like object
只需將傳遞的對象轉成字符串就可以了。
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))