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.
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
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))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With