Posted By

ndegruchy on 07/06/06


Tagged

textmate python String console lambda output


Versions (?)

Who likes this?

5 people have marked this snippet as a favorite

ndegruchy
anayhk
fukami
taboularasa
atr000


Better Unicode Text Wrapping Function


 / Published in: Python
 

  1. # This recipe refers:
  2. #
  3. # http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/148061
  4.  
  5. import re
  6. rx=re.compile(u\"([\\u2e80-\\uffff])\", re.UNICODE)
  7.  
  8. def cjkwrap(text, width, encoding=\"utf8\"):
  9. return reduce(lambda line, word, width=width: \'%s%s%s\' %
  10. (line,
  11. [\' \',\'\\n\', \'\'][(len(line)-line.rfind(\'\\n\')-1
  12. + len(word.split(\'\\n\',1)[0] ) >= width) or
  13. line[-1:] == \'\\0\' and 2],
  14. word),
  15. rx.sub(r\'\\1\\0 \', unicode(text,encoding)).split(\' \')
  16. ).replace(\'\\0\', \'\').encode(encoding)

Report this snippet  

You need to login to post a comment.