jieba分词报错:AttributeError: 'float' object has no attribute 'decode'
程序员文章站
2022-03-26 19:32:34
...
jieba分词报错:AttributeError: ‘float’ object has no attribute ‘decode’
最近在做关于新闻报道的研究,利用jieba分词时,程序报错AttributeError: ‘float’ object has no attribute ‘decode’
原始代码
一下仅展示报错部分代码
content_S = []
current_segment = jieba.lcut(content)
if len(current_segment) > 1 and current_segment != '\n':
content_S.append(current_segment)
contents_clean = []
all_words = []
stopwords_lst = stopwords['stopword'].tolist()
执行代码后
C:\Users\Administrator\Anaconda3\lib\site-packages\jieba_compat.py in strdecode(sentence)
35 if not isinstance(sentence, text_type):
36 try:
—> 37 sentence = sentence.decode(‘utf-8’)
38 except UnicodeDecodeError:
39 sentence = sentence.decode(‘gbk’, ‘ignore’)
AttributeError: ‘float’ object has no attribute ‘decode’
这是因为所需要分词的文本中出现了数字的缘故,此时仅仅需要添加一个异常处理就可以正常进行了。
修改后代码
def drop_words(content):
"""去除停用词"""
content_S = []
try:
global current_segment
current_segment = jieba.lcut(content)
except AttributeError:
pass
if len(current_segment) > 1 and current_segment != '\n': # 换行符
content_S.append(current_segment)
contents_clean = []
all_words = []
stopwords_lst = stopwords['stopword'].tolist()
因为我也是新手小白,遇到bug的时候第一反应找百度,发现有人也遇到了相同的问题,按照他的解决办法我的程序依然报错,于是只能自己想了……希望可以解决大家的问题。
推荐阅读
-
解决Keras报错AttributeError: 'NoneType' object has no attribute 'inbound_nodes'
-
使用torchsummary时报错AttributeError: ‘list‘ object has no attribute ‘size‘
-
python报错:AttributeError: 'module' object has no attribute 'xfeatures2d'
-
wsgiref报错AttributeError: 'NoneType' object has no attribute 'split'
-
反向关联报错AttributeError: ‘ForeignKey‘ object has no attribute ‘rel‘从django源码找答案~
-
python-pip升级报错- AttributeError: 'NoneType' object has no attribute 'bytes'
-
python多线程下报错:AttributeError: 'module' object has no attribute '_strptime'
-
用tkinter时,想要Entry的值输出,调用了get(),报错:AttributeError: 'NoneType' object has no attribute 'get'
-
AttributeError: 'str' object has no attribute 'decode'
-
使用torchsummary时报错AttributeError: ‘list‘ object has no attribute ‘size‘