[转]正则匹配时对象必须为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))
posted @   bingo彬哥  阅读(6352)  评论(0)    收藏  举报
努力加载评论中...
本站总访问量9104879
点击右上角即可分享
微信分享提示