레이블이 Character encoding인 게시물을 표시합니다. 모든 게시물 표시
레이블이 Character encoding인 게시물을 표시합니다. 모든 게시물 표시

2014. 1. 8.

Python String concat and Charset


I found first method to use for string-concat. but  It makes errors for non-romantic characters. So, I use second method by using join.


1) using StringIO

# coding:utf-8
from cStringIO import StringIO
txt = StringIO()

txt.write("AAAAA")
txt.write("BBBBB")
txt.write("?????")  # non romantic  characters will occur Error~

other_function( txt.getvalue())

txt.close()
  
2) using join  and list

# coding:utf-8
txt = []

txt.append("AAAAA")
txt.append("BBBBB")
txt.append("??????")  # non romantic characters will not occur Error.

other_function( "".join(txt))



3) using Stream  ## update  ( thank you~, Justin )

source = cStringIO.StringIO()
wrapper = codecs.getwriter("utf8")(source)
wrapper.writelines(unicode("AAAA "))
wrapper.writelines(unicode("non romantic => 비 로만틱"))

other_funcion( source.getvalue().decode("utf-8") )  

soruce.close()

Enhanced by Zemanta

2013. 9. 10.

Load non utf-8 file and display QTextEdit at pyhon



1) declare coding

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

2) load file  ( let's asume file is  'euc-kr')
   
     fileData = f.read()  # read from file


3) decode  to unicode
    unicodedata = unicode(fileData,"euc-kr")

4) encode 'utf-8'

    utf8data = unicodedata.encode("utf-8")

5) set text
    editor.setPlainText(utf8data)  # editor is QTextEdit


** summary ** : converting flow

    EUC-KR --->   UNICODE --> UTF-8

Enhanced by Zemanta