如何使用Unicode破折号进行字符串格式化?

8
我正在尝试使用Unicode变量进行字符串格式化。例如:
>>> x = u"Some text—with an emdash."
>>> x
u'Some text\u2014with an emdash.'
>>> print(x)
Some text—with an emdash.
>>> s = "{}".format(x)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
UnicodeEncodeError: 'ascii' codec can't encode character u'\u2014' in position 9: ordinal not in range(128)

>>> t = "%s" %x
>>> t
u'Some text\u2014with an emdash.'
>>> print(t)
Some text—with an emdash.

您可以看到我有一个unicode字符串,它可以正常打印。问题是当我使用Python的新(和改进?)format()函数时,就会出现问题。如果我使用旧的样式(使用%s),一切都正常,但当我使用{}format()函数时,它会失败。
您有任何想法为什么会发生这种情况吗?我正在使用Python 2.7.2。

在Windows上,如果你输出到控制台,你可能会遇到类似这样的异常,所以你应该非常小心。 - sorin
3个回答

9

新的format()在混合 ASCII 和 Unicode 字符串时不太宽容... 所以试试这样:

s = u"{}".format(x)

虽然这是有道理的,但旧的“%”方式更方便地处理了它,这很烦人。明示、隐式,等等,但仍然很恼人。 - 2rs2ts
使用Python v3.8.2似乎不需要'u'。 - David Medinets

4

我用以下方法效果很好。这是其他答案的变体。

>>> emDash = u'\u2014'
>>> "a{0}b".format(emDash)
'a—b'

3
同样的方式。
>>> s = u"{0}".format(x)
>>> s
u'Some text\u2014with an emdash.'

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接