2017-05-01 14 views
3

このエラーに関する複数の投稿を読みましたが、まだ解決できません。関数をループしようとすると、型エラー:TypeError:期待される文字列またはバイト様のオブジェクトが得られます。ここで期待される文字列またはバイト状のオブジェクト

def fix_Plan(location): 
    letters_only = re.sub("[^a-zA-Z]", # Search for all non-letters 
          " ",   # Replace all non-letters with spaces 
          location)  # Column and row to search 


    words = letters_only.lower().split() 

    stops = set(stopwords.words("english")) 

    meaningful_words = [w for w in words if not w in stops] 

    return (" ".join(meaningful_words)) 


col_Plan = fix_Plan(train["Plan"][0]) 

num_responses = train["Plan"].size 

clean_Plan_responses = [] 

for i in range(0,num_responses): 
    clean_Plan_responses.append(fix_Plan(train["Plan"][i])) 

はエラーです:あなたがコメントで述べたように

Traceback (most recent call last): 
    File "C:/Users/xxxxx/PycharmProjects/tronc/tronc2.py", line 48, in <module> 
    clean_Plan_responses.append(fix_Plan(train["Plan"][i])) 
    File "C:/Users/xxxxx/PycharmProjects/tronc/tronc2.py", line 22, in fix_Plan 
    location) # Column and row to search 
    File "C:\Users\xxxxx\AppData\Local\Programs\Python\Python36\lib\re.py", line 191, in sub 
    return _compile(pattern, flags).sub(repl, string, count) 
TypeError: expected string or bytes-like object 
+4

エラーが発生した場合は、*常にスタックトレース*を含む完全なエラーを送信します。 –

+2

'print(train [" Plan "] [i])'とそれが何であるかを見てください。 forループで 'fix_Plan()'の呼び出しの前に行います。私は 'train [" Plan "i]'があなたが期待していたものだとは思わない。 – abccd

+0

あなたはtry:を追加することができます。場所: – oshaiken

答えて

6

、一部の値は浮動小数点数ではなく文字列であるように思われました。 re.subに渡す前に文字列に変更する必要があります。最も簡単な方法はre.subを使用する場合locationstr(location)に変更することです。それがすでにstrであっても、それをとにかく行うことを傷つけることはありません。

letters_only = re.sub("[^a-zA-Z]", # Search for all non-letters 
          " ",   # Replace all non-letters with spaces 
          str(location)) 
+0

それはまさに問題の原因です。ありがとう – imanexcelnoob

関連する問題