【问题标题】:Using format to fill and justify multi-byte Unicode strings in Python 2.7在 Python 2.7 中使用格式填充和对齐多字节 Unicode 字符串
【发布时间】:2015-10-15 05:15:54
【问题描述】:

在 Python 中,很容易使用 string.format 填充(即填充)字符串并将它们向左、向右或居中对齐。例如:

>>> word = "Resume"
>>> print "123456890\n{0:>{1}}".format(word, 10)
>>> print len(name)

1234567890
    Resume
6    

但是,如果字符串包含多字节 Unicode 字符,string.format 不会正确计算字符串的宽度:

>>> word = u"Résumé"
>>> print "123456890\n{0:>{1}}".format(word.encode('utf8'), 10)
>>> print len(name.encode('utf8'))

1234567890
  Résumé
8

解决方案使用unicodedata.normalize('NFC', string),您可能已经阅读过。这确实将normalize Unicode character 序列(在某些情况下也可能是必要的!)但它确实不会导致string.format 正确计算字符串的编码宽度以输出到终端。 p>

那么如何在 Python 2.7 中使用string.format 打印正确填充/填充的字符串?

【问题讨论】:

    标签: python string python-2.7 unicode


    【解决方案1】:

    事实证明,答案非常简单:使用 Unicode 文字格式字符串:

    >>> word = u"Résumé"
    >>> print u"123456890\n{0:>{1}}".format(word, 10)
    >>> print len(name)
    
    1234567890
        Résumé
    6
    

    这个单字符解决方案似乎隐藏在 Python 错误跟踪器上 Victor Stinner 的 message 中:

    哦,顺便说一句,在 Python 2 中解决这个问题很简单:只需使用 Unicode 格式字符串。例如,将'{0}'.format(u'\u3042') 替换为u'{0}'.format(u'\u3042')

    我没有在任何 StackOverflow 答案中找到这个,也没有在 Google 上找到的任何页面上找到这个,无论是博客、论坛、邮件列表等。所以就在这里!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-01-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-03-08
      • 2021-01-31
      相关资源
      最近更新 更多