在python中对齐日语字符 [英] Aligning Japanese characters in python

查看:54
本文介绍了在python中对齐日语字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我很难在python中对齐日语字符.

代码:

 打印"{c1} {名称:> 14s} {c2} {昵称:> 14s} {飞机:> 16s}".format(名称=名称,昵称=昵称,飞机=飞机,c1 = u.color ['yellow'],c2 = u.color ['default']) 

结果:

如果字符串仅包含英文和数字,则如右图所示,.format()可以正常工作.

遇到日语字符时对齐错误,如左图所示.

有趣的是,与 {name:> 14s} 对齐时:

  • 如果名称"包含4个JP字符,则将有2个前缀空格.
  • 如果名称"包含3个JP字符,则将有5个前缀空格.
  • 如果名称"包含2个JP字符,则将有8个前缀空格.
  • 如果名称"包含0个JP字符,则将有14个前缀空格.

在这种情况下,它似乎可以处理1个日本字符= 3个空格.

{name:< 14s} {name:^ 14s} {name:> 14s} 均具有上述行为

我正在使用OSX 10.10.2,终端字体为monaco.

也许这与全角/半角字符有关.

反正像英语字符一样对齐日语字符吗?

谢谢.


伊格纳西奥·巴斯克斯(Ignacio Vazquez-Abrams)的答案确实是正确的方法.

  • 使用Python处理unicode的每个人都应阅读.第二,改用全角空格.

     >>>打印'{:> 8s}'.format('ありがとう')ありがとう>>>打印u'{:> 8s}'.format(u'ありがとう')ありがとう>>>打印u'{:\ u3000> 8s}'.format(u'ありがとう')ありがとう 

    I have difficulty aligning Japanese characters in python.

    Code:

    print "{c1}{name:>14s}{c2}{nick_name:>14s}{planes:>16s}".format(
        name=name, nick_name=nick_name, planes=planes, 
        c1=u.color['yellow'], c2=u.color['default']
    )
    

    Result:

    If the string contains english and numbers only, the .format() works fine,as shown on the right.

    The aligning goes wrong when encountering Japanese characters, as shown on the left.

    Interestingly, when aligning with {name:>14s}:

    • If "name" contains 4 JP characters, there would be 2 prefix spaces.
    • If "name" contains 3 JP characters, there would be 5 prefix spaces.
    • If "name" contains 2 JP characters, there would be 8 prefix spaces.
    • If "name" contains 0 JP characters, there would be 14 prefix spaces.

    It seems like it treat 1 Japanese charater = 3 spaces in this case.

    {name:<14s} {name:^14s} {name:>14s} all have the behavior mentioned above.

    I am using OSX 10.10.2, terminal font is monaco.

    Maybe this has something to do with full-width/half-width characters.

    Is there anyway to align Japanese characters just like English characters?

    Thanks you.


    Edit:

    Ignacio Vazquez-Abrams's answer is indeed the correct way.

    • Everyone who is dealing with unicode in Python should read the slide he pointed out.

    • The "\u3000" is the full-width space in CJK. See this page.

    • Review the .Format Syntax would also help.

    • I would also like to recommend this SO answer which helps me understanding how unicode works in Python.

    However, if the string contains both half-width and full-width characters, the alignment still goes wrong. A simple workaround is to use all full-width characters.

    解决方案

    You're performing two goofups simultaneously:

    1. You're using a UTF-8 sequence of bytes instead of a sequence of characters.
    2. You're aligning using half-width spaces.

    For the first, use unicodes instead of strs. For the second, use full-width spaces instead.

    >>> print '{:>8s}'.format('ありがとう')
    ありがとう
    >>> print u'{:>8s}'.format(u'ありがとう')
       ありがとう
    >>> print u'{:\u3000>8s}'.format(u'ありがとう')
       ありがとう
    

    这篇关于在python中对齐日语字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆