AttributeError: module 'fuzzywuzzy' has no attribute 'ratio'

AttributeError: module 'fuzzywuzzy' has no attribute 'ratio'

我正在尝试调用库 fuzzywuzzy 中的 ratio() 函数来匹配两个字符串并得到以下错误消息:

AttributeError: module 'fuzzywuzzy' has no attribute 'ratio'

版本有变化吗?我试图在 fuzz 中寻找其他函数以查看它是否存在,但我无法找到它。

import fuzzywuzzy as fuzz
from fuzzywuzzy import process
import Levenshtein
fuzz.ratio('Lord of the Rings', 'The Lord of the Rings')

如果您检查 here,您会发现您没有正确导入 fuzzywuzzy。

您应该使用 from fuzzywuzzy import fuzz

而不是 import fuzzywuzzy as fuzz

基本上,您有两种方法可以做到这一点。或者:

import fuzzywuzzy as <something>
<something>.fuzz.ratio(...)

或 从 fuzzywuzzy 导入 fuzz fuzz.ratio(...)

将导入更改为:

from fuzzywuzzy as fuzz
from fuzzywuzzy import process

ratiofuzzywuzzy.fuzz的一个方法。使用:

from fuzzywuzzy import fuzz

那么你可以使用:

fuzz.ratio('Lord of the Rings', 'The Lord of the Rings')

https://github.com/seatgeek/fuzzywuzzy 的自述文件指出 fuzzywuzzy 的使用方法是:

from fuzzywuzzy import fuzz
from fuzzywuzzy import process

您完成它的方式 (import fuzzy-wuzzy as fuzz) 意味着您需要调用层次结构中的另一个级别,fuzz.fuzz.blah 而不仅仅是 fuzz.blah

所以答案是要么使用那个额外的级别,要么以记录的方式导入它。