[转]正则匹配时对象必须为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