具有零宽度和双宽度字符的字符串的宽度,在终端的Python 3中(不在GUI中)

时间:2018-02-03 14:14:01

标签: python string python-3.x curses

我想格式化字符串以使用curses显示它们。 为此,如果行太短,我想添加空格,如果它太长则剪切它并添加延续字符。

我的问题是计算终端中字符串的实际宽度。在我的字符串中,我可以拥有不会被显示的字符和其他长度为2的字符(并且打印为正方形)。因此,函数len不是正确的候选者。

要解决此问题,我尝试使用unicodedate.category来过滤字符:

removed_category = ('Mn', 'So')
string = ''.join(char for char in string \
    if unicodedata.category(char) not in removed_category)

但它并不适用于所有情况。例如,' S' S'被检测为大写字母但在终端中它的宽度不是一个。 而且,它很慢而且不是很优雅。

我还尝试string.printable删除了可以在我的终端中显示的字符。

编辑(因为问题已经结束)

基于unicodedata.east_asian_width的解决方案不适用于零宽度字符。如果我们将其与unicodedata.category结合使用,它似乎有效:

def stringWidth(string):
    width = 0
    for c in string:
        # For zero-width characters
        if unicodedata.category(c)[0] in ('M', 'C'):
            continue
        w = unicodedata.east_asian_width(c)
        if w in ('N', 'Na', 'H', 'A'):
            width += 1
        else:
            width += 2

    return width

根据您的文字,您可能需要为零宽度字符添加其他类别。

0 个答案:

没有答案