Programming/Python
python - 인코딩 관련
bbolmin
2014. 3. 2. 19:31
[example]
"테스트"라는 문자열 ---> '\xc5\xd7\xbd\xba\xc6\xae'
a = '\xc5\xd7\xbd\xba\xc6\xae'
b = u'\xc5\xd7\xbd\xba\xc6\xae'
a의 type은 str
b의 type은 unicode
str 타입인 경우에는 decode해서 unicode 타입이 되고
unicode 타입인 경우에는 encode해서 str 타입이 된다.
ex)
u'some value'.encode('some value') -> ''
'some value'.decode('some value') -> u''
- a.decode('cp949')
- b.encode('utf8')
- 동일 함수 -
#codecs.unicode_escape_decode
#codecs.unicode_escape_encode
[ b를 a와 같은 형태로 바꿔줄 때 ]
b = b.encode('unicode_escape') # '\\xc5\\xd7\\xbd\\xba\\xc6\\xae'
b = b.decode('string-escape') # '\xc5\xd7\xbd\xba\xc6\xae'