[issue12568] Add functions to get the width in columns of a character

STINNER Victor report at bugs.python.org
Fri Oct 14 03:28:35 CEST 2011


STINNER Victor <victor.stinner at haypocalc.com> added the comment:

> There might be something you can steal from  ...

I don't think that Python should reinvent the wheel. We should just reuse wcswidth().

Here is a simple patch exposing wcswidth() function as locale.width().

Example:

>>> import locale
>>> text = '\u3042\u3044\u3046\u3048\u304a'
>>> len(text)
5
>>> locale.width(text)
10
>>> locale.width(' ')
1
>>> locale.width('\U0010abcd')
1
>>> locale.width('\uDC80')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
locale.Error: the string is not printable
>>> locale.width('\U0010FFFF')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
locale.Error: the string is not printable

I don't think that we need locale.width() on Windows because its console has already bigger issues with Unicode: see issue #1602. If you want to display correctly non-ASCII characters on Windows, just avoid the Windows console and use a graphical widget.

----------
keywords: +patch
Added file: http://bugs.python.org/file23401/locale_width.patch

_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue12568>
_______________________________________


More information about the Python-bugs-list mailing list