什么是unicode_literals用于? [英] What is unicode_literals used for?

查看:90
本文介绍了什么是unicode_literals用于?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Python中遇到了一个有问题的 __ future __。unicode_literals 。代码如下:

 #encoding:utf-8 
#from __future__ import unicode_literals
name ='hellówörldfrom example'
print name

我得到正确的输出,但是当我这样做:

 #encoding:utf-8 
from __future__ import unicode_literals
name ='hellówörld从例子'
打印名称

我有错误:

  UnicodeEncodeError:'ascii'编解码器不能在位置4中编码字符u'\xf3':ordinal不在范围内(128)

所以我想知道是 unicode_literals 将每个字符串编码为utf- 8?
我该怎么办来重写这个错误?

解决方案

你的终端或控制台没有让Python知道支持UTF-8。



没有来自__future__导入unicode_literals 行的行,您正在构建一个包含UTF的字节串-8编码字节。使用字符串,您正在构建一个 unicode 字符串。



print 不得不对这两个值进行不同的对待;字节字符串写入 sys.stdout 不变。一个 unicode 字符串首先被编码为字节,并且Python请参考 sys.stdout.encoding 。如果你的系统没有正确地告诉Python它支持什么编解码器,默认是使用ASCII。



你的系统没能告诉Python使用什么编解码器? sys.stdout.encoding 设置为ASCII,并编码 unicode 值以打印失败。



您可以在打印时通过手动编码为UTF-8来验证:

 #encoding :utf-8 
from __future__ import unicode_literals
name ='hellówörldfrom example'
print name.encode('utf8')
pre>

,您可以通过在__future __ 导入语句中创建unicode文字而不使用来重现问题:

 #encoding:utf-8 
name =u'hellówörldfrom example'
print name

其中 u'..'也是一个unicode字面值。



没有关于你的环境的细节,很难说解决方案是什么;这很大程度上取决于使用的操作系统和控制台或终端。


I get a wired problem with __future__.unicode_literals in Python. The codes is as following:

# encoding: utf-8
# from __future__ import unicode_literals
name = 'helló wörld from example'
print name

I get right output, but when I do this:

# encoding: utf-8
from __future__ import unicode_literals
name = 'helló wörld from example'
print name

I got error:

UnicodeEncodeError: 'ascii' codec can't encode character u'\xf3' in position 4: ordinal not in range(128)

So I am wondering that is unicode_literals encode every string as an utf-8? And what should I do to override this error?

解决方案

Your terminal or console is failing to let Python know it supports UTF-8.

Without the from __future__ import unicode_literals line, you are building a byte string that holds UTF-8 encoded bytes. With the string you are building a unicode string.

print has to treat these two values differently; a byte string is written to sys.stdout unchanged. A unicode string is encoded to bytes first, and Python consults sys.stdout.encoding for that. If your system doesn't correctly tell Python what codec it supports, the default is to use ASCII.

Your system failed to tell Python what codec to use; sys.stdout.encoding is set to ASCII, and encoding the unicode value to print failed.

You can verify this by manually encoding to UTF-8 when printing:

# encoding: utf-8
from __future__ import unicode_literals
name = 'helló wörld from example'
print name.encode('utf8')

and you can reproduce the issue by creating unicode literals without the from __future__ import statement too:

# encoding: utf-8
name = u'helló wörld from example'
print name

where u'..' is a unicode literal too.

Without details on what your environment is, it is hard to say what the solution is; this depends very much on the OS and console or terminal used.

这篇关于什么是unicode_literals用于?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆