[轉]正則匹配時對象必須為string or bytes-like object


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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM