[python] UnicodeDecodeError: 'utf-8' codec can't decode byte 0xb1 in position 3: invalid start byte 에러

Apic·2023년 3월 21일
0

코딩

목록 보기
10/14
with open(txt_file, 'r', encoding='utf-8') as f:
	text = f.readline()[:-1]
    text = re.sub(pattern, ' ', text).strip()
    text = re.sub(r'\s+', ' ', text)

    result.append(text)

이런식으로 텍스트 파일을 열고 읽을려고 하는데 오류가 발생했다.
UnicodeDecodeError: 'utf-8' codec can't decode byte 0xb1 in position 3: invalid start byte

해결방법

  1. utf-8이 아닌 euc-kr 로 해보기
with open(txt_file, 'r', encoding='euc-kr') as f:
	text = f.readline()[:-1]
    text = re.sub(pattern, ' ', text).strip()
    text = re.sub(r'\s+', ' ', text)

    result.append(text)
  1. 프로그램으로 텍스트 파일을 utf-8로 변경하기
    redutf8이라고 검색하면 프로그램을 다운로드 할 수 있다.

    여기서 폴더 찾기로 텍스트 파일이 들어있는 폴더를 선택해 주고 변환 확장자에서 txt만 남긴 다음 UTF8Encode를 하면 된다.
profile
코딩 공부하는 사람

0개의 댓글