8

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.

2 Answers 2

15

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
Sign up to request clarification or add additional context in comments.

Comments

8

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))

4 Comments

I also get a status of 'Na' if the character isn't Asian.
@MarkRansom All Unicode characters have an East Asian Width.
It seems my version of Python (2.7.5) differs from yours then. I just double checked: >>> print unicodedata.east_asian_width(u'x') Na
I also see value 'W' from unicodedata.east_asian_width(u'你'). Any ideas?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.