【问题标题】:AttributeError: 'float' object has no attribute 'strip' (how to remove this error)AttributeError: \'float\' object has no attribute \'strip\' (如何消除这个错误)
【发布时间】:2023-01-23 17:40:09
【问题描述】:

文本预处理

reviews=[comment.strip() for comment in reviews.comment] # remove both the leading and the trailing characters
reviews=[comment for comment in reviews if comment] # removes empty strings, because they are considered in Python as False
reviews[0:10]

输出 :

AttributeError: 'float' object has no attribute 'strip'

当我尝试在 jupyter 中执行此代码时,显示以下错误,任何知道如何解决的人。删除浮动或条带也会显示错误。

原始输出应显示客户的评论。

【问题讨论】:

  • 好吧,在不知道评论内容是什么的情况下,您可以将其强制为字符串,如下所示:str(comment).strip()
  • 解决此问题的方法是限制使用条()字符串对象

标签: python dataframe jupyter css-float variable-assignment


【解决方案1】:

该错误表明在您的笔记本中reviews.commentlist,但它显然不仅包含字符串,还包含浮点数。这会立即产生您看到的错误。

也许首先检查为什么你的 review.comment 中有一个浮动,并确保它有充分的理由并且没有错误。

此外,如果浮点数没问题,只需将其转换为字符串:

reviews=[str(comment).strip() for comment in reviews.comment] # remove both the leading and the trailing characters

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-04-06
    • 1970-01-01
    • 2022-12-04
    • 1970-01-01
    • 2022-12-01
    • 2022-12-03
    • 2022-01-19
    • 2016-11-25
    相关资源
    最近更新 更多