use*_*031 3 python preprocessor attributeerror
我正在尝试使用预处理器库来清理存储在 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'
Run Code Online (Sandbox Code Playgroud)
您可能preprocessor还安装了该模块,该模块与该模块完全不同tweet-preprocessor。然而,令人困惑的是,该import preprocessor as p语句可以用于两者。安装这两个模块后,Python 会忽略tweet-preprocessor并自动选择preprocessor,它不包含clean函数,因此会收到错误。
为了解决这个问题,我必须使用以下命令卸载这两个模块:
pip uninstall preprocessor
pip uninstall tweet-preprocessor
Run Code Online (Sandbox Code Playgroud)
然后我关闭所有 shell 以重新开始并输入:
pip install tweet-preprocessor
Run Code Online (Sandbox Code Playgroud)
最后:
>>> import preprocessor as p
>>> p.clean('#this and that')
'and that'
Run Code Online (Sandbox Code Playgroud)
仅仅卸载preprocessor并不起作用。尽管已卸载,Python 仍继续导入该模块。我不知道为什么,但我怀疑这与 Python 在后台保存的缓存有关。