How to control padding of Unicode string containing east Asia characters
When trying to line up ASCII text with Chinese in fixed-width font, there is a set of full width versions of the printable ASCII characters. Below I made a translation table of ASCII to full width version:
# coding: utf8
# full width versions (SPACE is non-contiguous with ! through ~)
SPACE = '\N{IDEOGRAPHIC SPACE}'
EXCLA = '\N{FULLWIDTH EXCLAMATION MARK}'
TILDE = '\N{FULLWIDTH TILDE}'
# strings of ASCII and full-width characters (same order)
west = ''.join(chr(i) for i in range(ord(' '),ord('~')))
east = SPACE + ''.join(chr(i) for i in range(ord(EXCLA),ord(TILDE)))
# build the translation table
full = str.maketrans(west,east)
data = '''\
蝴蝶(A song)
心之城(Another song)
支持你的爱人(Yet another song)
根生的种子
鸽子歌(Cucurrucucu palo whatever)
林地之间
蓝光
在你眼里
肖邦离别曲
西行(魔戒王者再临主题曲)(Into something)
深陷爱河
钟爱大地
时光流逝
卡农
舒伯特小夜曲(SERENADE)
甜蜜的摇篮曲(Sweet Lullaby)
'''
# Replace the ASCII characters with full width, and create a song list.
data = data.translate(full).rstrip().split('\n')
# translate each printable line.
print(' ----------Songs-----------'.translate(full))
for i,song in enumerate(data):
line = '|{:4}: {:20.20}|'.format(i+1,song)
print(line.translate(full))
print(' --------------------------'.translate(full))
Output
----------Songs-----------
| 1: 蝴蝶(A song) |
| 2: 心之城(Another song) |
| 3: 支持你的爱人(Yet another s|
| 4: 根生的种子 |
| 5: 鸽子歌(Cucurrucucu palo|
| 6: 林地之间 |
| 7: 蓝光 |
| 8: 在你眼里 |
| 9: 肖邦离别曲 |
| 10: 西行(魔戒王者再临主题曲)(Into s|
| 11: 深陷爱河 |
| 12: 钟爱大地 |
| 13: 时光流逝 |
| 14: 卡农 |
| 15: 舒伯特小夜曲(SERENADE) |
| 16: 甜蜜的摇篮曲(Sweet Lullaby|
--------------------------
It's not overly pretty, but it lines up.
There seems to be no official support for this, but a built-in package may help:
>>> import unicodedata
>>> print unicodedata.east_asian_width(u'中')
The returned value represents the category of the code point. Specifically,
- W - East Asian Wide
- F - East Asian Full-width (of narrow)
- Na - East Asian Narrow
- H - East Asian Half-width (of wide)
- A - East Asian Ambiguous
- N - Not East Asian
This answer to a similar question provided a quick solution. Note however, the display result depends on the exact monospaced font used. The default fonts used by ipython and pydev don't work well, while windows console is ok.
Take a look at kitchen. I think it might have what you want.