Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Checking a character is fullwidth or halfwidth in Python

Tags:

python

I want to check whether a character is fullwidth or halfwidth using Python

string="你好hallo"
for char in string:
    if( \uFF60- \u0f01  and \uFFE0-\uFFE6 ): print( char +"is fullwidth")
    elif(\uFF61-\uFFDC and \uFFE8-\uFFEE):print(char+ " is halfwidth")

Please help me to change this pseudocode into real python code.

like image 877
showkey Avatar asked Apr 14 '14 11:04

showkey


2 Answers

As Alex Thornton mentioned, using unicodedata.east_asian_width() is right. However, it has the following returned values:

# East_Asian_Width (ea)

ea ; A         ; Ambiguous
ea ; F         ; Fullwidth
ea ; H         ; Halfwidth
ea ; N         ; Neutral
ea ; Na        ; Narrow
ea ; W         ; Wide

Returned values of 'W', 'F' and 'A' should be considered as full-width on Windows.

Reference: http://www.unicode.org/reports/tr44/tr44-4.html#Validation_of_Enumerated


On POSIX platform, the quote characters (u'“' and u'”') are considered as ambiguous, which are actually 1 character width in console. For console usage, you may try a 3rd-party library urwid:

>>> from urwid.util import str_util
>>> str_util.get_width(ord(u'x'))
1
>>> str_util.get_width(ord(u'“'))
1
>>> str_util.get_width(ord(u'你'))
2
like image 81
Rockallite Avatar answered Sep 28 '22 19:09

Rockallite


You can check the width of the character using unicodedata.east_asian_width(unichr):

import unicodedata

for char in string:
    status = unicodedata.east_asian_width(char)
    if status == 'F':
         print('{0} is full-width.'.format(char))
    elif status == 'H':
        print('{0} is half-width.'.format(char))
like image 44
anon582847382 Avatar answered Sep 28 '22 20:09

anon582847382