如何控制包含东亚字符的Unicode字符串的填充

如何控制包含东亚字符的Unicode字符串的填充,第1张

如何控制包含东亚字符的Unicode字符串的填充

尝试以固定宽度字体将ASCII文本与中文对齐时,存在一组可打印ASCII字符的全角版本。下面我制作了一张ASCII到全角版本的转换表:

    # 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 = '''Butterfly (a song)Another songSupport your lover (yet another song)Rooted seedsCucurrucucu Palo whateverBetween woodlandsBlu rayIn your eyesChopin's farewellJourney to the WestDeep in loveLove the earthTime goes byCannonSerenade by SchubertSweet 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))
输出量
 ----------Songs-----------|   1: Butterfly (asong)                 ||   2: anothersong       ||    3: support your lovers  ||   4:the root seeds               ||   5:cucurrucupalo       ||   between woodlands               ||   7: Blu ray                 ||   8: in your eyes              ||   9: Chopin's farewell song         ||  10: Journey to the West          ||  11: deep in love               ||  12: love the earth             ||  13: time goes by              ||  14: Canon                  ||  15: Serenade      ||  16: sweet lullaby     | --------------------------

它不是太漂亮,但是排列整齐。



欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5630272.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-16
下一篇 2022-12-16

发表评论

登录后才能评论

评论列表(0条)

保存