Fuzzywuzzy报错:'module'对象无'extractone'属性求助
解决fuzzywuzzy中AttributeError: 'module' object has no attribute 'extractone'的问题
我看你遇到了fuzzywuzzy调用时的属性错误,结合你的代码和场景,这大概率是方法名写错或者调用方式不对导致的,下面一步步帮你解决:
你的场景还原
你有这样的DataFrame:
alertname custname 0 wlison wilson 1 dais said 2 4dams adams 3 ad4ms adams 4 ad48s adams 5 smyth smith 6 smythe smith 7 gillan gillan 8 gilen gillan 9 scott-smith scottsmith 10 scott smith scottsmith 11 perrson person 12 persson person
运行这段代码时出错:
from fuzzywuzzy import process from fuzzywuzzy import fuzz import Levenshtein key=['wilson'] process.extractone(key, choices=alertname,scorer=fuzz.token_sort_ratio,limit=10)
错误提示:AttributeError: 'module' object has no attribute 'extractone'
核心问题与解决步骤
1. 方法名写错了!这是最关键的原因
fuzzywuzzy的process模块里的方法是**extractOne(首字母大写O)**,而不是你写的extractone(全小写o),这直接导致了找不到属性的错误!
2. 修正参数格式
另外还有两个小问题需要调整:
key应该是单个字符串,而不是列表['wilson'],extractOne接受单个查询值choices需要传递可迭代的序列(比如列表),如果alertname是DataFrame的一列,要用alertname.tolist()或者alertname.values来转换
3. 修正后的完整代码
from fuzzywuzzy import process from fuzzywuzzy import fuzz import Levenshtein # 把key改成单个字符串 key = 'wilson' # 将DataFrame列转换为列表作为choices matches = process.extractOne(key, choices=alertname.tolist(), scorer=fuzz.token_sort_ratio) print(matches)
4. 额外:版本兼容方案(如果还是报错)
如果你的fuzzywuzzy版本较旧,确实没有extractOne方法,可以用extract方法配合limit=1来实现相同效果:
matches = process.extract(key, choices=alertname.tolist(), scorer=fuzz.token_sort_ratio, limit=1) # 取第一个匹配结果 if matches: print(matches[0])
5. 确保依赖包是最新版
如果还是有问题,建议升级fuzzywuzzy和配套的python-Levenshtein:
pip install --upgrade fuzzywuzzy python-Levenshtein
内容的提问来源于stack exchange,提问作者user2906657
相关产品推荐
相关产品推荐

