Python9 Python file open read error(f.readline()UnicodeDecodeError) python에서 파일을 읽을 때 아래와 같이 에러가 나올 때가 있다. line = f.readline() UnicodeDecodeError: 'cp949' codec can't decode byte 0xec in position 515: illegal multibyte sequence 원인은 파일을 읽을 때 코덱이 맞지 않아서이다. 기본적으로 python3는 ANSI를 기준으로 작성된 파일을 읽는다. 따라서, UTF-8로 작성된 파일을 읽으려고 할 때 일반적인 open(filename, 'r') 을 사용한다면 위와 같은 에러가 발생한다. 해결하는 방법으로는 아래와 같이 encoding='utf-8' 을 추가한다. with open('test.html', 'r', encoding='utf8') as f: .. 2020. 10. 18. 이전 1 2 3 다음 반응형