2021-09-05

TypeError: expected string or bytes-like object

   4136             else:
   4137                 values = self.astype(object)._values
-> 4138                 mapped = lib.map_infer(values, f, convert=convert_dtype)
   4139
   4140         if len(mapped) and isinstance(mapped[0], Series):

pandas\_libs\lib.pyx in pandas._libs.lib.map_infer()

<ipython-input-13-5098f813e7d3> in review_without_stop(review)
      5     all_stop_words = set(stop_word) # 删除停用词中重复的项
      6     # 短评中的非中文字符替换为''
----> 7     review = re.sub("[^\u4e00-\u9fa5]",'',review)
      8     # 去除全角空白字符
      9     review = review.replace("\u3000","")

c:\program files\python37\lib\re.py in sub(pattern, repl, string, count, flags)
    190     a callable, it's passed the Match object and must return
    191     a replacement string to be used."""
--> 192     return _compile(pattern, flags).sub(repl, string, count)
    193
    194 def subn(pattern, repl, string, count=0, flags=0):

TypeError: expected string or bytes-like object

遇到这个问题时,可以根据代码执行的步骤寻找问题,根据箭头显示,找到问题所在review = re.sub("[^\u4e00-\u9fa5]",'',review),类型出错,所以只要改成review = re.sub("[^\u4e00-\u9fa5]",'',str(review)),就可以就是将数据类型改成字符型
我真的上网搜了好多答案,但是居然是str()加错了位置,记录一下,希望也可以帮助跟我一样遇到困难的人。

上一篇:python爬取某趣阁小说(2.0),十分钟爬完一千六百章


下一篇:day12_04 mysql外键单文件