2016-08-17 61 views
0

我得到了经典的错误:的Python如何解决错误的Unicode字符串

ascii' codec can't decode byte 0xc3 in position 28: ordinal not in range(128)

这个时候,我解决不了。这个错误来自该行:

mensaje_texto_inmobiliaria = "%s, con el email %s y el teléfono %s está se ha contactado con Inmobiliar" % (nombre, email, telefono) 

具体而言,从teléfono字。我曾尝试将# -*- coding: utf-8 -*-添加到文件的开头,并添加unicode(<string>)<string>.encode("utf-8")。没有工作。任何建议都会有帮助。

+0

尝试将'from __future__ import unicode_literals'添加到文件顶部,是否解决了您的问题起诉? – Thtu

+0

你运行的是什么版本的Python?我有2.7.12,它工作正常,它只是将该字符解释为字符串文字中的转义序列 –

+0

@ThomasTu谢谢你,......解决了它。为什么? – alejoss

回答

3

这是回应为何这解决了问题OP是有,并在这个问题上OP somebackground试图描述

from __future__ import unicode_literals 
from builtins import str 

在默认的IPython的2.7内核:

(IPython的会话)

在Python 2.7值得注意的
In [1]: type("é") # By default, quotes in py2 create py2 strings, which is the same thing as a sequence of bytes that given some encoding, can be decoded to a character in that encoding. 
Out[1]: str 

In [2]: type("é".decode("utf-8")) # We can get to the actual text data by decoding it if we know what encoding it was initially encoded in, utf-8 is a safe guess in almost every country but Myanmar. 
Out[2]: unicode 

In [3]: len("é") # Note that the py2 `str` representation has a length of 2. There's one byte for the "e" and one byte for the accent. 
Out[3]: 2 

In [4]: len("é".decode("utf-8")) # the py2 `unicode` representation has length 1, since an accented e is a single character 
Out[4]: 1 

一些其他的事情:

  • "é"是一回事str("é")
  • u"é"是一回事"é".decode('utf-8')unicode("é", 'utf-8')
  • u"é".encode('utf-8')是一回事str("é")
  • 您通常调用解码与PY2 str,与PY2 unicode编码。
    • 由于早期的设计问题,您可以调用两者,即使这没有任何意义。
    • 在python3中,与python2 unicode相同的str不能再被解码,因为根据定义字符串是经过解码的字节序列。默认情况下,它使用utf-8编码。
  • 使用ascii编解码器编码的字节序列的行为与其解码对应行为完全相同。
    • 在python 2.7中没有将来导入:type("a".decode('ascii'))给出了一个unicode对象,但是它的行为与str("a")几乎完全相同。 python3中并不是这种情况。

随着中说,这里就是上面的代码片段做:

  • __future__是由Python核心团队backports中python3功能python2,让您使用内python3成语保持一个模块python2。
  • from __future__ import unicode_literals具有以下作用:
    • 没有未来进口"é"是一回事str("é")
    • 随着未来进口"é"在功能上是一样的东西unicode("é")
  • builtins是一个模块这是由核心Python团队批准的,并且包含在python2 python3 api中使用python3成语的安全别名。
    • 由于超出我的原因,封装本身被命名为“未来”,因此要安装这个builtins模块运行:pip install future
  • from builtins import str具有以下作用:
    • str构造现在给出你认为它所做的事情,即python2 unicode对象形式的文本数据。因此,它在功能上同样的事情str = unicode
    • 注:Python3 str在功能上相同Python2 unicode
    • 注:要获得字节,可以使用“字节”的前缀,例如b'é'

外卖是这样的:

  1. 读/解码早早就和写/编码编码在最后解码
  2. 使用str对象的字节数和文本unicode对象
+0

'u“é”.encode('utf-8')'与'str(“é”)'不一致' –

+0

这是很好的知道,你认为你可以解释'u “é”.encode('utf-8')== str(“é”)'? – Thtu

+0

double equals调用特殊方法'__eq__'来检查是否相等。在这两个对象上它最有可能说它们是相等的,因为它们都是'' –

相关问题