Can';在Python2.7.x中对西里尔字符进行t编码

Can';在Python2.7.x中对西里尔字符进行t编码,python,encoding,character-encoding,Python,Encoding,Character Encoding,当我试图编码西里尔字母“ª”字符时,我会出错。以下是我的代码和错误: >>> "Р".encode('utf8') Traceback (most recent call last): File "<stdin>", line 1, in <module> UnicodeDecodeError: 'ascii' codec can't decode byte 0xd0 in position 0: ordinal not in range(128)

当我试图编码西里尔字母“ª”字符时,我会出错。以下是我的代码和错误:

>>> "Р".encode('utf8')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
UnicodeDecodeError: 'ascii' codec can't decode byte 0xd0 in position 0: ordinal not in range(128)
我从演讲中得到了上述方法。它在终端和简单python文件中工作。它在OpenERP中不起作用。

Python2.x中的
(例如
str
)中的任何内容都已编码。您需要先将其解码为
unicode
,然后才能将其编码为其他内容


Python的解释器以仅ascii模式启动,因此您不能直接输入Cyrlic字符。相反,您可以通过其代码点编号创建它们:

>>> print unichr(0x420)
Р
>>> unichr(0x420).encode('utf-8')
'\xd0\xa0'
或以他们的名字:

>>> u'\N{CYRILLIC CAPITAL LETTER ER}'.encode('utf-8')
'\xd0\xa0'

更好的是,如果我们从一个文本开始,首先创建一个Unicode文本(
u“ª”
),它在终端和简单python文件上工作。但它现在正在OpenERP上工作。请看我编辑的幻灯片。PowerPoint幻灯片似乎不是让事情变得神秘的好方法。
>>> u'\N{CYRILLIC CAPITAL LETTER ER}'.encode('utf-8')
'\xd0\xa0'