遍历数组时发生ValueError

时间:2018-10-12 14:50:22

标签: python linux windows fuzzywuzzy

我和一个朋友正在使用FuzzyWuzzy进行近似字符串匹配(假设是哈哈),以从JSON(具体来说是Steam API JSON)生成的数组中查找给定的输入。

这是我们用来从JSON生成数组的代码,其中path是保存在磁盘上的JSON的路径,而array_location只是为了提取游戏ID和游戏名称。

def array_from_json_file(path, array_location):
    array = {}
    with open(path, encoding='utf-8') as f:
        array = json.load(f)
    array = enumerate_json(array, array_location)
    return array

这是遍历整个列表以进行模糊匹配的代码,其中query是用户输入,data是我们从JSON生成的数组,而str_location是所以只搜索名称而不是ID

def fuzzy_query(query, data, str_location):
    results = []
    for i in range(0, len(data)):
        target = enumerate_json(data[i], str_location)
        if (len(target) > 1):
            strength = fuzz.partial_ratio(query.lower(), target.lower()) # This is where the issue is
            results.append((strength, data[i]))
    return results

最后,当我运行脚本时得到的回溯

Traceback (most recent call last):
  File "main.py", line 81, in <module>
    main()
  File "main.py", line 73, in main
    query_results = fuzzy_query(query, games, ["name"])
  File "main.py", line 45, in fuzzy_query
    strength = fuzz.partial_ratio(query.lower(), target.lower())
  File "C:\Program Files (x86)\Microsoft Visual Studio\Shared\Python36_64\lib\site-packages\fuzzywuzzy\utils.py", line 38, in decorator
    return func(*args, **kwargs)
  File "C:\Program Files (x86)\Microsoft Visual Studio\Shared\Python36_64\lib\site-packages\fuzzywuzzy\utils.py", line 29, in decorator
    return func(*args, **kwargs)
  File "C:\Program Files (x86)\Microsoft Visual Studio\Shared\Python36_64\lib\site-packages\fuzzywuzzy\utils.py", line 47, in decorator
    return func(*args, **kwargs)
  File "C:\Program Files (x86)\Microsoft Visual Studio\Shared\Python36_64\lib\site-packages\fuzzywuzzy\fuzz.py", line 47, in partial_ratio
    blocks = m.get_matching_blocks()
  File "C:\Program Files (x86)\Microsoft Visual Studio\Shared\Python36_64\lib\site-packages\fuzzywuzzy\StringMatcher.py", line 59, in get_matching_blocks
    self._str1, self._str2)
ValueError: apply_edit edit operations are invalid or inapplicable

我的朋友和我在Python版本(v3.6.6)和FuzzyWuzzy版本(v0.17.0)中都具有相同的版本。当我运行Windows 10 LTSB时,他正在使用Ubuntu。他在运行我们的脚本时没有任何问题。据我们所知,没有环境差异会导致此问题。

0 个答案:

没有答案