TypeError: expected string or bytes-like object'

Viewed 6478

I am running this code in python with FuzzyWuzzy which returns me this error:

TypeError: ('expected string or bytes-like object', 'occurred at index CONCAT')

Is there a fast easy way to avoid that error ? My file contains some Int like 142 Aberdeen street. I guess that's where the error code came from.

    import pandas as pd
    from fuzzywuzzy import fuzz
    from fuzzywuzzy import process
    import csv
    import os


    #DEFINE AND CONFIGURE
    FULL_MATCHING_THRESHOLD = 80
    PARTIAL_MATCHING_THRESHOLD = 100
    SORT_MATCHING_THRESHOLD = 100
    TOKEN_MATCHING_THRESHOLD = 100
    MAX_MATCHES=1

    #READ THE CURRENT DATABASE
    companies_db = "C://Users//Dell/Desktop//Fuzzy_reconcile//TEST_DUP.csv"
    pwd = os.getcwd()
    os.chdir(os.path.dirname(companies_db))
    current_db_dataframe = pd.read_csv(os.path.basename(companies_db),skiprows=1,index_col=False, names=['CONCAT'])
    os.chdir(pwd)

    def find_matches(matchThis):
        rows = current_db_dataframe['CONCAT'].values.tolist();
        rows.remove(matchThis)
        matches= process.extractBests(matchThis,rows,scorer=fuzz.ratio,score_cutoff=FULL_MATCHING_THRESHOLD,limit=MAX_MATCHES)
        if len(matches)==0:
            matches= process.extractBests(matchThis,rows,scorer=fuzz.partial_ratio,score_cutoff=PARTIAL_MATCHING_THRESHOLD,limit=MAX_MATCHES);
            if len(matches)==0:
                matches= process.extractBests(matchThis,rows,scorer=fuzz.token_set_ratio,score_cutoff=TOKEN_MATCHING_THRESHOLD,limit=MAX_MATCHES);
                if len(matches)==0:
                    matches= process.extractBests(matchThis,rows,scorer=fuzz.token_sort_ratio,score_cutoff=SORT_MATCHING_THRESHOLD,limit=MAX_MATCHES);

        return matches[0][0] if len(matches)>0 else None


    fn_find_matches = lambda x: find_matches(x)
    current_db_dataframe['Duplicate']=current_db_dataframe.applymap(fn_find_matches)

    current_db_dataframe.to_csv("results.csv")

Error Message:

 File "C:\ProgramData\Anaconda\lib\site-packages\fuzzywuzzy\utils.py", line 95, in full_process
    string_out = StringProcessor.replace_non_letters_non_numbers_with_whitespace(s)

  File "C:\ProgramData\Anaconda\lib\site-packages\fuzzywuzzy\string_processing.py", line 26, in replace_non_letters_non_numbers_with_whitespace
    return cls.regex.sub(" ", a_string)

TypeError: ('expected string or bytes-like object', 'occurred at index CONCAT')
1 Answers

you can remove character of your string by following regex

number=re.sub("[^a-zA-Z]",  # Search for all non-letters
       " ",                 # Replace all non-letters with spaces
       str(string))
Related