파이썬에서 파일을 저장할때 encoding
을 설정해주지 않으면 윈도우 환경에서는 파일을 열었을때 괴상한 문자가 나온다.
물론 linux 계열인 mac에서도 encoding
을 잘못 설정하면 괴상한 문자가 나오기는 당연!
Encoding
- encoding은 os마다 다르다. 윈도우에서는
cp949
- linux에서는
utf8
을 사용 - 아래 코드에서
encoding
을 설정하는곳이 있는데
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import pandas as pd | |
df = {}.... | |
df.to_csv(filename, encoding='cp949') |
- 위처럼 윈도우에서 파일을 열어볼 예정이라면
encoding=cp949
를 넘겨줘야 한다. - linux 계열에서 파일을 열어볼 예정이라면
encoding
을 설정할 필요가 없다. - 설정할 필요가 없는 이유는 ? pandas to_csv()를 보면
defulat: utf-8
이기 때문이다.