- 증상
# 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)
'''
'프로그래밍 관련 > [Python] 간단 해결' 카테고리의 다른 글
[Python] glob 정규식으로 폴더 및 파일 제외 (0) | 2020.05.22 |
---|---|
[Python] PermissionError: [Errno 13] Permission denied: Path... (0) | 2020.05.21 |
[Python] cv2.imread 한글 경로 인식 문제 (0) | 2020.05.21 |
[Python] json.dumps() 한글 > 유니코드로 저장될 때 (0) | 2020.04.22 |
[Python] OSError: mysql_config not found (Ubuntu 16.04) (0) | 2020.04.20 |