Passion/Python

Python encoding 관련

sunshout 2009. 10. 26. 23:14
utf-8 파일을 읽을 때

import codecs
fileObj = codecs.open( "someFile", "r", "utf-8")
u = fileObj.read() # 파일에 있는 UTF-8 바이트 들이 유니코드 문자열로 넘어옴

euc-kr 을 utf-8로 인코딩 할 때
hanguel = unicode('한글', 'euc-kr').encode('utf-8')

utf-8 을 euc-kr로 인코딩 할 때
unicode(filename).encode('euc-kr')

윈도우 Python은 euc-kr 인코딩을 가정하고 있고, pyqt4 와 같은 프로그램은 utf-8을 기본으로 가정

따라서  Python 과 PyQt 간의 인코딩을 맞춰줘야 한다.