'utf-8' codec can't decode byte 0xff in position 0: invalid start byte

우주먼지·2021년 11월 29일
0

이와 같은 에러가 발생했을 때

f = open("korean/txt/4CM00085.txt", 'r',encoding='utf-8')
cnt = 0
while True:
    line = f.readline()
    if not line: break
    print(line)
f.close()

다음과 같이 utf-8로 해서 에러가 발생하는 것이었다.

이는 파일의 첫번째 바이트가 0xFF여서 에러가 발생하는 것으로 utf-8의 형태를 보이지 않는다.

이 경우는 간단한 방법으로 해결이 가능한데, utf-8 ⇒ utf-16으로 바꿔주면 된다.

f = open("korean/txt/4CM00085.txt", 'r',encoding='utf-16')
cnt = 0
while True:
    line = f.readline()
    if not line: break
    print(line)
f.close()
profile
안녕하세요 ㅎㅎ

0개의 댓글