首页 > 解决方案 > 运行 Fuzzywuzzy 字符串匹配器脚本的问题

问题描述

该脚本应该从测试文件中获取字符串并将它们与主文件进行比较,并将匹配结果作为输出文件中的结果。

我遇到的问题是运行它不会产生所述输出文件。

# -*- coding: utf-8 -*-
"""
Test - String matching
"""
#%%
from fuzzywuzzy import process
import pandas as pd
import os

#%%

def StringMatch (master, testfile, num_match=3):
    master_names = master.iloc[:,3]
    test_names = testfile.iloc[:,0]    
    fhp_new = [process.extract(x, master_names, limit=limit) for x in test_names]
    lab=" "
    i=1
    while i<=num_match:
        lab = lab + " " + "Match" + str(i)
        i = i+1
    aggregated_matches = pd.DataFrame(fhp_new, columns = lab.split())
    d={}
    for x in range (1, num_match + 1):
        d["Match{0}".format(x)] = [y[0] for y in aggregated_matches["Match" + str(x)]]
    d["test_original"] = test_names
    d["perfect match"] = d["Match1"] == d["test_original"]
    out = pd.DataFrame(data=d)
    out.to_csv(str(outFile + ".csv"))
    return (out)
    print ("finished")

master = pd.read_csv("MasterVendorDevice.csv")
testfile = pd.read_csv("testfile.csv", encoding='latin-1')
limit=3

baseDir = os.path.join("/Users", "Tim", "Desktop", "String Matcher")
outDir = os.path.join(baseDir, "out")

if not os.path.exists(outDir):
    os.makedirs(outDir)

outFile = os.path.join(outDir, "matches")

标签: pythonpython-3.x

解决方案


推荐阅读