Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/328.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何使用unicode emdash进行字符串格式化?_Python_Unicode_String Formatting - Fatal编程技术网

Python 如何使用unicode emdash进行字符串格式化?

Python 如何使用unicode emdash进行字符串格式化?,python,unicode,string-formatting,Python,Unicode,String Formatting,我正在尝试使用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 "<

我正在尝试使用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.
>x=u“一些带有emdash的文本。”
>>>x
“某些文本\u2014带有一个破折号。”
>>>打印(x)
一些带有破折号的文本。
>>>s=“{}”格式(x)
回溯(最近一次呼叫最后一次):
文件“”,第1行,在
UnicodeEncodeError:“ascii”编解码器无法对位置9中的字符u'\u2014'进行编码:序号不在范围内(128)
>>>t=“%s”%x
>>>t
“某些文本\u2014带有一个破折号。”
>>>打印(t)
一些带有破折号的文本。
你可以看到我有一个unicode字符串,它打印得很好。问题是当我使用Python的新的(和改进的?)函数
format()
时。如果我使用旧样式(使用
%s
),一切正常,但是当我使用
{}
format()
函数时,它会失败

你知道为什么会这样吗?我使用的是Python2.7.2。

当您混合使用ASCII和unicode字符串时,新的
格式()
就不那么宽容了。。。所以试试这个:

s = u"{}".format(x)
同样的方式

>>> s = u"{0}".format(x)
>>> s
u'Some text\u2014with an emdash.'

使用以下方法对我很有效。这是其他答案的变体

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

在Windows上应该非常小心,因为如果输出到console,可能会出现这样的异常。虽然这是有道理的,但旧的
%
方式更方便地处理它,这很烦人。显式、隐式、yada-yada,但仍然令人恼火。使用PythonV3.8.2,似乎不需要“u”。