How to get a "screen" length of a multibyte string?

Hans Mulder hansmu at xs4all.nl
Sun Nov 25 06:31:04 EST 2012


On 25/11/12 11:19:18, kobayashi wrote:
> Hello,
>> Under platform that has fixed pitch font,
> I want to get a "screen" length of a multibyte string
>> --- sample ---
> s1 = u"abcdef"
> s2 = u"あいう" # It has same "screen" length as s1's.
> print len(s1) # Got 6
> print len(s2) # Got 3, but I want get 6.
> --------------
>> Abobe can get a "character" length of a multibyte string.
> Is there a way to get a "screen" length of a multibyte string?

How about:
from unicodedata import east_asian_width
def screen_length(s):
 return sum(2 if east_asian_width(c) == 'W' else 1 for c in s)
Hope this helps,
-- HansM


More information about the Python-list mailing list

AltStyle によって変換されたページ (->オリジナル) /