栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

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

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

如何控制包含东亚字符的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     | --------------------------

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



转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/624236.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号