annyoung

python 윈도우 한글문제 본문

프로그래밍

python 윈도우 한글문제

nopsled 2016. 4. 26. 21:30
아오 인코딩 때문에 4시간은 삽질한거 같다.



문제점_____________________________________


1. 윈도우 cmd 쉘에서는 한글이 자꾸 깨진다.

2. raw_input으로 입력받아서 urllib.quote()로 url encoding해주면 윈도우와 리눅스는 다른 결과값이 나온다.




주절주절___________________________________


# -*- coding: utf-8 -*-

import sys

reload(sys)

sys.setdefaultencoding('utf-8')

print u'한글' # 고정으로 변수에 한글을 넣을 경우 맨앞에 u를 붙인다.

print repr(raw_input('input korean : ')) # 윈도우 cmd 쉘에서 "한글" 단어를 입력할시 '\xc7\xd1\xb1\xdb'로 보여진다.

#print repr(raw_input('input korean : ')) # 리눅스 bash 쉘에서 "한글" 단어를 입력할시 '\xed\x95\x9c\xea\xb8\x80'로 보여진다.



삽질하다가 알게된건 다음과 같다.

윈도우 cmd 쉘에서 입력 받은 한글(\xc7\xd1\xb1\xdb)은 euc-kr로 되어 있는 것이고,

리눅스 bash 쉘에서 입력 받은 한글( \xed\x95\x9c\xea\xb8\x80)은 utf-8로 되어 있는 것이다.




해결방법___________________________________


# -*- coding: utf-8 -*-

import sys

reload(sys)

sys.setdefaultencoding('utf-8')

print repr(raw_input('input korean : ').decode('euc-kr').encode('utf-8')) # '\xed\x95\x9c\xea\xb8\x80' 결과는 결국 utf-8로 나오고 잘 출력된다.









'프로그래밍' 카테고리의 다른 글

[javascript] get ip only using javascript  (5) 2018.04.19
python ctypes GetFileVersion  (0) 2016.11.09
python unicode unescape (html unescape)  (0) 2015.11.07
django standard install..  (0) 2015.10.13
Convert Facebook username to id  (0) 2015.04.12
Comments