python计算unicode字符转折合半角字符数
1 import unicodedata
2
3 def get_unicode_halfwidth(ustr):
4 if type(ustr) != unicode:
5 raise TypeError, "not a unicode string"
6
7 halfwidth = 0
8 for uchar in ustr:
9 width_tag = unicodedata.east_asian_width(uchar)
10 halfwidth += 2 if (width_tag == 'W') or (width_tag == 'F') else 1
11 return halfwidth