expected string or bytes-like object
今天在进行去停用词时遇到的问题,使用正则匹配时出现的。原因已查明,是由于有字节而非字符串的内容存在。解决办法:
对传入的字符串进行强制类型转化为str
#原函数:
def drop_punctuation(text):
punc = '~`!#$%^&*()_+-=|\';"":/.,?><~·!@#¥%……&*()——+-=“:’;、。,?》《{} \n'
new_text=re.sub(r"[%s]+" %punc, "",text) #原输入
return new_text
#更改后:
def drop_punctuation(text):
punc = '~`!#$%^&*()_+-=|\';":/.,?><~·!@#¥%……&*()——+-=“:’;、。,?》《{} \n'
new_text=re.sub(r"[%s]+" %punc, "",str(text)) #强制转换str
return new_text