2017-04-26 2 views
0

토큰이 들어있는 목록의 목록이 있습니다. 일부 토큰에는 빈 공간으로 대체하려는 특수 문자가 포함되어 있습니다. 그러나 내 코드 didnt 한 작업 :파이썬 문자열 바꾸기가 작동하지 않습니다.

mylist = [['hi','how','are','you','forward\xcato\xcahearing\xcafrom\xcayou\xcasoon'], 
      ['good','morning']] 

mycode :

new_list = [] 
for l in mylist: 
    l2 = [i.replace('\xca', ' ') for i in l] 
    new_list.append(l2) 
new_list[0] 
>>> ['hi','how','are','you','forward\xcato\xcahearing\xcafrom\xcayou\xcasoon'] 

이 일을 일부러 왜 확실하지.

+0

. 출력 : [안녕하세요, '어떻게', '있습니다', '너', '앞으로 곧 듣고 싶다'] – manelfp

+0

메신저 jupyter notebook 사용 – jxn

+0

파이썬 2.7에서도 작동합니다. – roganjosh

답변

0

이 너무 Jupyter 작동합니다

mylist = [['hi','how','are','you','forward\xcato\xcahearing\xcafrom\xcayou\xcasoon'], 
      ['good','morning']] 

print ([[sentence.translate(str.maketrans("\xca", " ")) for sentence in item] for item in mylist]) 
0

내 코드는 문자열에서 모든 특수 문자를 제거합니다! '\ xca'뿐만 아니라 줄을 다듬을 수 있습니다. (어떤 라이브러리 필요 없음) :

your_list = [['hi','how','are','you','forward\xcato\xcahearing\xcafrom\xcayou\xcasoon'], 
['good','morning']] 


def special_characters_finder(text): 

    renew_word = [] 

    for char in text: 
     try: 
      char.encode('ascii') 

     except UnicodeEncodeError: 
      renew_word.append(' ') 

     else: 
      renew_word.append(char) 

    return ''.join(renew_word) 


buffer_output = [] 
for box in your_list: 
    for item in box: 
     get_list = special_characters_finder(item) 
     buffer_output.append(get_list) 

print(buffer_output) 

OUTPUT : 나는 파이썬 3.5.x의를 사용하고 있는데 그것은 나를 위해 작동

['hi', 'how', 'are', 'you', 'forward to hearing from you soon', 'good', 'morning'] 
관련 문제