python - 标记句子时出现AttributeError
问题描述
当我尝试以下代码时:
tok_corp= [nltk.word_tokenize(sent.decode('utf-8')) for sent in corpus]
我得到一个AttributeError
:
'float' 对象没有属性 'decode'
解决方案
在将所有内容链接到单行之前,请尝试检查您的对象类型,例如
for sent in corpus:
print(type(sent), sent)
你应该看到float
里面有东西。
接下来str.decode('utf8')
有点危险。如果您在 Python3 中,则 utf8 应该是默认值,因此如果您在 Python2 中,代码中的某处应该有一个open()
,而不是在 Python2 中使用默认值,则不需要使用open()
utf8 来指定编码io.open()
,例如
import io
with io.open('somefile.txt', 'r', encoding='utf8') as fin:
corpus = fin.read().split('\n')
这样,作为fin
对象读取的所有内容默认都是str
/unicode
类型,因此不需要 `.decode('utf8') 。
推荐阅读
- javascript - 在 React 应用程序中使用 Google OAuth2 刷新/使用刷新令牌后保持用户登录
- c# - 如何使用 IHtmlGenerator 对 Tag Helper 进行单元测试
- r - 在 R 中使用 pROC 的随机森林拟合对象的 ROC 曲线,使用正或负“投票”作为预测器
- qr-code - 寻找有关自定义可读代码的文档
- java - 只接受 8 位数字
- azure - 使用用户凭据从窗口桌面应用程序调用 azure 资源管理器存储 API
- python - 使用 selenium Select 导航下拉菜单
- java - 在 TestNG 中执行时 XML 中出现启动错误 - 无法继续执行测试 - Selenium
- python - 如何编写在其内部连接中使用复杂“on”子句的 Django 查询?
- php - PHP curl 在 5.6 和 5.4 (Centos7) 中损坏