티스토리 뷰

 

"UnicodeEncodeError: 'utf-8' codec can't encode characters in position : surrogates not allowed"

 

기존의 데이터 파일을 전처리한 후 저장하려는 코드에서 에러가 발생했다.

구글링해보니 다양한 방법들이 나오는데, 나의 경우는 기존 데이터 파일에 존재하는 유니코드 문자가 원인이었다.

따라서, 저장하기 전에 전체 텍스트에 대해 다음 코드로 유니코드 문자를 제거함으로써 에러를 해결했다.

if not text.isascii():
    text = ''.join([char for char in text if char.isascii()]).strip()

 

반응형

댓글