13

我在 Windows XP SP3 上使用最新的 IPython 运行 python 2.6,我有两个问题。我的第一个问题是,在 IPython 下,我无法直接输入 Unicode 字符串,因此无法打开非拉丁名称的文件。让我演示一下。在通常的python下这有效:

>>> sys.getdefaultencoding()
'ascii'
>>> sys.getfilesystemencoding()
'mbcs'
>>> fd = open(u'm:/Блокнот/home.tdl')
>>> print u'm:/Блокнот/home.tdl'
m:/Блокнот/home.tdl
>>>

顺便说一句,里面是西里尔字母。在 IPython 下我得到:

In [49]: sys.getdefaultencoding()
Out[49]: 'ascii'

In [50]: sys.getfilesystemencoding()
Out[50]: 'mbcs'

In [52]: fd = open(u'm:/Блокнот/home.tdl')
---------------------------------------------------------------------------
IOError                                   Traceback (most recent call last)

C:\Documents and Settings\andrey\<ipython console> in <module>()

IOError: [Errno 2] No such file or directory: u'm:/\x81\xab\xae\xaa\xad\xae\xe2/home.tdl'

In [53]: print u'm:/Блокнот/home.tdl'
-------------->print(u'm:/Блокнот/home.tdl')
ERROR: An unexpected error occurred while tokenizing input
The following traceback may be corrupted or invalid
The error message is: ('EOF in multi-line statement', (15, 0))

---------------------------------------------------------------------------
UnicodeEncodeError                        Traceback (most recent call last)

C:\Documents and Settings\andrey\<ipython console> in <module>()

C:\Program Files\Python26\lib\encodings\cp866.pyc in encode(self, input, errors)
     10
     11     def encode(self,input,errors='strict'):
---> 12         return codecs.charmap_encode(input,errors,encoding_map)
     13
     14     def decode(self,input,errors='strict'):

UnicodeEncodeError: 'charmap' codec can't encode characters in position 3-9: character maps to <und

In [54]:

第二个问题不那么令人沮丧,但仍然如此。当我尝试打开文件并将文件名参数指定为非 unicode 字符串时,它不会打开。在打开文件之前,我必须强制解码来自 OEM 字符集的字符串,这非常不方便:

>>> fd2 = open('m:/Блокнот/home.tdl'.decode('cp866'))
>>>

也许它与我的区域设置有关,我不知道,因为我什至无法从控制台剪切和粘贴西里尔文字。我在区域设置中到处都放了“俄语”,但它似乎不起作用。

4

3 回答 3

12

是的。在控制台输入 Unicode 总是有问题的,通常最好避免,但IPython 尤其糟糕。它转换您在其控制台上键入的字符,就好像它们是在 ISO-8859-1 中编码的一样,而不管您给它的实际编码是什么。

现在,你不得不说u'm:/\u0411\u043b\u043e\u043a\u043d\u043e\u0442/home.tdl'

于 2010-02-14T11:11:48.677 回答
1

反常的是,这将起作用:

fd = open('m:/Блокнот/home.tdl')

或者:

fd = open('m:/Блокнот/home.tdl'.encode('utf-8'))

这通过将字符串作为原始 UTF-8 编码字节字符串输入来绕过 ipython 的错误。ipython 不会尝试任何有趣的事情。然后,您可以随意将其编码为 un​​icode 字符串,然后继续您的生活。

于 2011-04-14T16:28:26.667 回答
0

我对希腊语输入也有同样的问题,这个来自启动板的补丁也适用于我。

谢谢。

于 2010-11-10T10:55:49.533 回答