使用默认的等宽字体在终端窗口中对齐

使用默认的等宽字体在终端窗口中对齐

本文介绍了使用默认的等宽字体在终端窗口中对齐 unicode 文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在从网络中提取数据并希望将其与终端窗口中的表格对齐.在大多数情况下,我可以很好地对齐文本,但是当文本包含某些符号或外来字符时,事情就会变得混乱.我该如何处理这些字符?以下是输出第三行出现问题的示例:

>>>items = "苹果树", "香蕉植物", "Orange 으르", "Goodbye">>>值 = 100、200、300、400>>>对于 i, v in zip(items, values):... 打印 "%-15s : %-4s" % (i, v)...苹果树:100香蕉植物:200橙色 으르 : 300再见:400>>>

注意:我正确引用了所有项目."Orange" 右引号在 Stack Overflow 上没有正确显示,但在终端窗口中显示正常.

更新:我为这个问题增加了悬赏.我正在寻找一种无需太多额外代码且无需使用外部库即可实现的解决方案.它也应该适用于 python 2.7+ 和 3.x(测试版本和应用不同修复的条件会很好).此外,它不应该要求任何额外的系统配置或更改字体或更改标准 Debian/Ubuntu 安装的任何终端设置.

解决方案

可以使用 东亚宽度 属性来自他们的 Unicode 数据.从 以编程方式告诉如果一个 Unicode 字符在终端中占用多个字符空间并使用该值进行对齐:

#!/usr/bin/python3导入 unicodedataitems = "苹果树", "香蕉植物", "Orange 으르", "Goodbye"值 = 100、200、300、400对于 i, v in zip(items, values):eawid = len(i) + sum(1 for v in i if unicodedata.east_asian_width(v) == 'W')垫 = ' ' * (15 - eawid)打印("%s%s : %-4s" % (i, pad, v))

给出:

苹果树:100香蕉植物:200橙色 으르 : 300再见:400

如果您的浏览器对这些字符使用 1.5 宽的字形,这可能会出现错位;在我的终端中,plan으르 的宽度完全相同.

这里的语法是 Python 3,但同样的技术适用于 2.7.

I am pulling data from the web and want to align it in a table in a terminal window. I can align the text fine in most cases but when the text contains certain symbols or foreign characters things get messy. How can I handle these characters? Here is an example with the problem on the third line of output:

>>> items = "Apple tree", "Banana plant", "Orange 으르", "Goodbye"
>>> values = 100, 200, 300, 400
>>> for i, v in zip(items, values):
...     print "%-15s : %-4s" % (i, v)
...
Apple tree      : 100
Banana plant    : 200
Orange 으르   : 300
Goodbye         : 400
>>>

Note: I quoted all the items correctly. The "Orange"closing quotes don't show correctly here on Stack Overflow but they display fine in the terminal window.

UPDATE: I have added a bounty to this question. I am looking for a solution that can be implemented without too much additional code and without using external libraries. It should also work with python 2.7+ and 3.x (conditionals that test for versions and apply different fixes would be fine). Also it should not require any additional system configuration or changing of fonts or changing any terminal settings of a standard Debian/Ubuntu installation.

解决方案

The special behaviour for those particular characters can be identified using the East Asian width property from their Unicode data. Taking the suggestion from Programmatically tell if a Unicode character takes up more than one character space in a terminal and using that value for alignment:

#!/usr/bin/python3

import unicodedata

items = "Apple tree", "Banana plant", "Orange 으르", "Goodbye"
values = 100, 200, 300, 400
for i, v in zip(items, values):
    eawid = len(i) + sum(1 for v in i if unicodedata.east_asian_width(v) == 'W')
    pad = ' ' * (15 - eawid)
    print("%s%s : %-4s" % (i, pad, v))

gives:

Apple tree      : 100
Banana plant    : 200
Orange 으르     : 300
Goodbye         : 400

This may appear misaligned if your browser is using a 1.5-width glyph for those characters; in my terminal, plan is exactly the same width as 으르.

Syntax here is Python 3, but the same technique works in 2.7.

这篇关于使用默认的等宽字体在终端窗口中对齐 unicode 文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-23 11:58