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