我使用的是Python 2.6.5。我的代码需要使用“大于或等于”符号。它是这样的:
>>> s = u'\u2265'
>>> print s
>>> ≥
>>> print "{0}".format(s)
Traceback (most recent call last):
File "<input>", line 1, in <module>
UnicodeEncodeError: 'ascii' codec can't encode character u'\u2265'
in position 0: ordinal not in range(128)`
为什么会出现这个错误?有没有正确的方法来做这件事?我需要使用.format()
函数。
发布于 2010-07-13 16:34:48
只需使第二个字符串也是unicode字符串
>>> s = u'\u2265'
>>> print s
≥
>>> print "{0}".format(s)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
UnicodeEncodeError: 'ascii' codec can't encode character u'\u2265' in position 0: ordinal not in range(128)
>>> print u"{0}".format(s)
≥
>>>
发布于 2010-07-13 16:35:36
unicode
%s需要unicode
格式的字符串。
>>> print u'{0}'.format(s)
≥
https://stackoverflow.com/questions/3235386
复制相似问题