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

댓글 없음:

댓글 쓰기