Better Unicode Text Wrapping Function

# This recipe refers:
#
#  <a href="http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/148061" >http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/148061</a>

import re
rx=re.compile(u"([u2e80-uffff])", re.UNICODE)

def cjkwrap(text, width, encoding="utf8"):
return reduce(lambda line, word, width=width: '%s%s%s' %
(line,
[' ','n', ''][(len(line)-line.rfind('n')-1
+ len(word.split('n',1)[0] ) >= width) or
line[-1:] == '' and 2],
word),
rx.sub(r'1 ', unicode(text,encoding)).split(' ')
).replace('', '').encode(encoding)

source

Leave a Reply