【问题标题】:AttributeError: module 'preprocessor' has no attribute 'clean'AttributeError:模块“预处理器”没有属性“干净”
【发布时间】:2022-01-06 05:49:05
【问题描述】:

我正在尝试使用预处理器库来清理存储在 Pandas 数据框中的文本。我安装了最新版本 (https://pypi.org/project/tweet-preprocessor/),但收到以下错误消息:

import preprocessor as p
#forming a separate feature for cleaned tweets
for i,v in enumerate(df['text']):
    df.loc[v,'text'] = p.clean(i)

---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-183-94e08e1aff33> in <module>
      1 #forming a separate feature for cleaned tweets
      2 for i,v in enumerate(df['text']):
----> 3     df.loc[v,'text'] = p.clean(i)

AttributeError: module 'preprocessor' has no attribute 'clean'

【问题讨论】:

  • 您的目录中或附近是否有一个名为preprocessor.py 的文件?

标签: python attributeerror preprocessor


【解决方案1】:

先尝试安装:

pip install tweet-preprocessor

然后:

import preprocessor as p

【讨论】:

    【解决方案2】:

    您可能还安装了preprocessor 模块,这与tweet-preprocessor 模块完全不同。然而,令人困惑的是,import preprocessor as p 语句可用于两者。安装这两个模块后,Python 会忽略 tweet-preprocessor 并自动选择 preprocessor,它不包含 clean 函数,因此您收到了错误。

    要解决这个问题,我必须使用以下命令卸载这两个模块:

    pip uninstall preprocessor
    pip uninstall tweet-preprocessor
    

    然后我关闭所有 shell 重新开始并输入:

    pip install tweet-preprocessor
    

    最后:

    >>> import preprocessor as p
    >>> p.clean('#this and that')
    'and that'
    

    仅仅卸载preprocessor 不起作用。尽管已卸载,Python 仍继续导入该模块。我不知道为什么,但我怀疑这与 Python 在后台保存的缓存有关。

    【讨论】:

      猜你喜欢
      • 2021-02-01
      • 2019-01-14
      • 1970-01-01
      • 2018-01-24
      • 2018-04-14
      • 2019-02-18
      • 1970-01-01
      • 2021-11-14
      • 2018-09-03
      相关资源
      最近更新 更多