[英]Python2.7 print unicode string still getting UnicodeEncodeError: 'ascii' codec can't encode character … ordinal not in range(128)
A simple print function 简单的打印功能
def TODO(message):
print(type(message))
print(u'\n~*~ TODO ~*~ \n %s\n ~*~\n' % message)
called like this 这样叫
TODO(u'api servisleri için input check decorator gerekiyor')
results in this error 导致此错误
<type 'unicode'>
Traceback (most recent call last):
File "/srv/www/proj/__init__.py", line 38, in <module>
TODO(u'api servisleri için input check decorator gerekiyor')
File "/srv/www/proj/helpers/utils.py", line 33, in TODO
print(u'\n~*~ TODO ~*~ \n %s\n ~*~\n' % message)
UnicodeEncodeError: 'ascii' codec can't encode character u'\xe7' in position 32: ordinal not in range(128)
But it works in ipython console 但是它可以在ipython控制台中工作
In [10]: TODO(u'api servisleri için input check decorator gerekiyor')
<type 'unicode'>
~*~ TODO ~*~
api servisleri için input check decorator gerekiyor
~*~
This works with python 2.7.12 but fails somehow with 2.7.9. 这适用于python 2.7.12,但由于2.7.9而以某种方式失败。
What is it that am i doing wrong here? 我在这里做错什么事?
Edit: function fails when called in a flask application, works in python console. 编辑:在flask应用程序中调用时函数失败,可在python控制台中使用。
Different terminals (and GUIs) allow different encodings. 不同的终端(和GUI)允许不同的编码。 I don't have a recent ipython handy, but it is apparently able to handle the non-ASCII 0xe7
character ( 'ç'
) in your string. 我没有最近使用过的ipython,但显然可以处理您字符串中的非ASCII 0xe7
字符( 'ç'
)。 Your normal console, however, is using the 'ascii'
encoding (mentioned by name in the exception), which can't display any bytes greater than 0x7f
. 但是,您的普通控制台使用的是'ascii'
编码(在名称中例外提及),该编码不能显示大于0x7f
任何字节。
If you want to print non-ASCII strings to an ASCII console, you'll have to decide what to do with the characters it can't display. 如果要将非ASCII字符串打印到ASCII控制台,则必须决定如何处理无法显示的字符。 The str.encode
method offers several options: str.encode
方法提供了几个选项:
str.encode([encoding[, errors]])
errors
may be given to set a different error handling scheme.errors
可给予设定了不同的错误处理方案。 The default forerrors
is'strict'
, meaning that encoding errors raise aUnicodeError
.errors
的默认值为'strict'
,这意味着编码错误会引发UnicodeError
。 Other possible values are'ignore'
,'replace'
,'xmlcharrefreplace'
,'backslashreplace'
and any other name registered viacodecs.register_error()
, see section Codec Base Classes . 其他可能的值是'ignore'
,'replace'
,'xmlcharrefreplace'
,'backslashreplace'
以及通过codecs.register_error()
注册的任何其他名称,请参见编解码器基类一节。
Here's an example that uses each of those four alternative error-handlers on your string (without the extra decoration added by TODO
): 这是一个示例,该示例在您的字符串上使用这四个替代错误处理程序中的每一个(不使用TODO
添加的额外修饰):
#!/usr/bin/env python2
# -*- coding: utf-8 -*-
from __future__ import print_function
uni = u'api servisleri için input check decorator gerekiyor'
handlers = ['ignore', 'replace', 'xmlcharrefreplace', 'backslashreplace']
for handler in handlers:
print(handler + ':')
print(uni.encode('ascii', handler))
print()
The output: 输出:
ignore:
api servisleri iin input check decorator gerekiyor
replace:
api servisleri i?in input check decorator gerekiyor
xmlcharrefreplace:
api servisleri için input check decorator gerekiyor
backslashreplace:
api servisleri i\xe7in input check decorator gerekiyor
Which one of those outputs comes closest to what you want is for you to decide. 这些输出中哪一个最接近您想要的,由您决定。
For more information, see the Python 2 " Unicode HOWTO ", and Ned Batchelder's " Pragmatic Unicode, or, How Do I Stop the Pain? ", also available as a 36 minute video from PyCon US 2012 . 有关详细信息,请参阅Python 2“ Unicode HOWTO ”和Ned Batchelder的“ 实用Unicode或如何停止疼痛? ”(也可从PyCon US 2012观看 36分钟的视频) 。
Edit : ...or, as you seem to have discovered, your terminal can display Unicode just fine, but your default encoding is nevertheless set to 'ascii'
, which is more restrictive than it needs to be. 编辑 :...或者,就像您似乎已经发现的那样,您的终端可以很好地显示Unicode,但是您的默认编码仍设置为'ascii'
,这比需要的限制更多。
\\xe7 \\ xe7
One of the utf-8 character that represents small 'ç'. 代表小“ç”的utf-8字符之一。 Python 2.7.9 probably encode with ASCII. Python 2.7.9可能使用ASCII编码。 You can run the code below in any version of Python that represents Python 2.7.9's behaviour. 您可以在代表Python 2.7.9行为的任何版本的Python中运行以下代码。
import sys;
# -*- coding: utf-8 -*-
def TODO(message):
print(type(message))
print(u'\n~*~ TODO ~*~ \n %s\n ~*~\n' % message)
message = u'api servisleri için input check decorator gerekiyor'
encodedMessage = message.encode('ascii')
print(sys.stdout.encoding)
TODO(encodedMessage)
It will throw the exception 它将引发异常
Traceback (most recent call last): File "test.py", line 9, in encodedMessage = message.encode('ascii') UnicodeEncodeError: 'ascii' codec can't encode character '\\xe7' in position 16: ordinal not in range(128) 追溯(最近一次通话):文件“ test.py”,第9行,位于encodingMessage = message.encode('ascii')UnicodeEncodeError:'ascii'编解码器无法对位置16处的字符'\\ xe7'进行编码范围内(128)
So, issue is related with interpreter's encoding rules. 因此,问题与解释器的编码规则有关。 You can encode on your own or ignore. 您可以自行编码或忽略。
Hope it will be useful 希望它会有用
Apparently, print function is a bit different from the print statement. 显然,打印功能与打印语句有点不同。
https://docs.python.org/2.7/library/functions.html#print https://docs.python.org/2.7/library/functions.html#print
All non-keyword arguments are converted to strings like
str() does and written to the stream, separated by sep
and followed by end.
Simply, encoding the unicode string solved it 只需编码unicode字符串即可解决
msg = u'\n~*~ TODO ~*~ \n %s\n ~*~\n' % message
print(msg.encode("utf-8"))
Still, not sure why it works with 2.7.12, maybe a locale thing? 仍然不确定为什么它能与2.7.12一起使用,可能是语言环境问题?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.