0

我正在尝试使用 Pyenchant 对名为 house 的熊猫数据框中名为 pets 的列中的每个条目进行拼写检查。

import enchant
dict = enchant.Dict("en_US")

for pets in house:
     [pets] = dict.suggest([pets])[0]

当我运行此代码时,我收到一个关于未将字节串传递给 Pyenchant 的错误。不知道该怎么办。完整的错误文本如下:

文件“myfile”,第 20 行,在 [pets] = dict.suggest([pets])[0] 文件“/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/enchant / init .py”,第 662 行,建议 word = self._StringClass(word) 文件“/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/enchant/utils.py” ,第 152 行,在的 raise Error("Don't pass bytestrings to pyenchant") enchant.errors.Error: Don't pass bytestrings to pyenchant

我怎样才能解决这个问题?谢谢。

4

1 回答 1

0

如果您的数据帧包含字节串,则需要先对它们进行解码,然后再将它们传递给enchant;你可以用.str.decode('utf-8'). 然后应用你的函数,处理这种情况的最干净的方法通常是map在你的系列中使用而不是迭代。(你也不应该隐藏关键字dict):

checker =  enchant.Dict("en_US")
house = pd.Series([b'caat', b'dogg'])

#decode the bytestrings
house = house.str.decode('utf-8')

#map the spell checker
house.map(lambda x: checker.suggest(x)[0])

# Out[19]:
# 0    cat
# 1    dog
# dtype: object
于 2016-09-15T01:43:58.360 回答