运行Fuzzywuzzy字符串匹配脚本的问题

时间:2018-05-07 21:05:54

标签: python python-3.x

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

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

# -*- 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")

0 个答案:

没有答案