【问题标题】:Capitalizing non-ASCII words in Python在 Python 中将非 ASCII 单词大写
【发布时间】:2009-06-17 11:24:42
【问题描述】:

如何在 Python 中将包含非 ASCII 字符的单词大写?有没有办法调整stringcapitalize() 方法来做到这一点?

【问题讨论】:

    标签: python unicode ascii capitalization


    【解决方案1】:

    使用 Unicode 字符串:

    # coding: cp1252
    print u"é".capitalize()
    # Prints É
    

    如果您只有一个 8 位字符串,请先将其解码为 Unicode:

    # coding: cp1252
    print "é".decode('cp1252').capitalize()
    # Prints É
    

    如果您再次需要它作为 8 位字符串,请对其进行编码:

    # coding: cp1252
    print "é".decode('cp1252').capitalize().encode('cp1252')
    # Prints É (assuming your terminal is happy to receive cp1252)
    

    【讨论】:

    【解决方案2】:

    capitalize() 应该 Just Work™ 用于 Unicode 字符串。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-04-04
      • 2020-09-15
      • 1970-01-01
      • 2017-04-28
      • 1970-01-01
      • 1970-01-01
      • 2017-05-25
      • 2018-08-06
      相关资源
      最近更新 更多