본문 바로가기

프로그래밍 관련/[Python] 간단 해결

[Python] UnicodeDecodeError: 'cp949' codec can't decode byte 0xe2 in position 17533: illegal multibyte sequence

- 증상

# CP949로 인코딩 되어있는 파일 Open시

with open(path, 'r') as f:
    f = f.read()
    
    print(f)

'''
Traceback (most recent call last):
  Error Message...
UnicodeDecodeError: 'cp949' codec can't decode byte 0xe2 in position 17533: illegal multibyte sequence
'''

 

- 해결

# Open시 Parameter(mode, encoding)를 통해 해결
# mode 'rt' = default

with open(path, 'rt', encoding='UTF-8') as f:
    f = f.read()
    
    print(f)

'''
File Read And Show Contents By Text
'''

 

- 참고 (open > mode parameter)

'''
========= =========================================================
Character Meaning
========= =========================================================
'r' open for reading (default)
'w' open for writing, truncating the file first
'x' create a new file and open it for writing
'a' open for writing, appending to the end of the file if it exists
'b' binary mode
't' text mode (default)
'+' open a disk file for updating (reading and writing)
'U' universal newline mode (deprecated)
'''