니다.sub erroring"예상되는 문자열이나 바이트 개"

I have read 여러 개의 게시물에 대한 이 오류는,하지만 나는 아직도 할 수 있't figure it out. 도 루프를 통해 기능:

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
질문에 대한 의견 (10)
해결책

으로 당신에 명시된 의견,일부 의 값을 나타난 것을 뜨지 않는,문자열입니다. 당신이 필요합니다 그것을 변경하는 문자열을 전달하기 전에re.sub. 가장 간단한 방법을 변경하는위치str(위)를 사용할 때re.sub. 그것은 것't 상하게 할것이 어쨌든 경우에도 그's 미str.

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

나는 가정 더 나은 사용하는 것입니다.일()함수입니다. 예를 들어 여기에있는 당신을 도움이 될 수 있습니다.

import re
import nltk
from nltk.tokenize import word_tokenize
nltk.download('punkt')
sentences = word_tokenize("I love to learn NLP \n 'a :(")
#for i in range(len(sentences)):
sentences = [word.lower() for word in sentences if re.match('^[a-zA-Z]+', word)]  
sentences
해설 (0)

가장 간단한 솔루션을 적용하는 파이썬 str 기능을 열려고 하는 루프를 통해.

사용하는 경우에는 팬더 이로 구현할 수 있습니다

데이터 프레임['테']=데이터 프레임['테'].적용(str)

해설 (0)