用Python在PowerShell中使用Unicode?窗户里的替代外壳?

2024-10-01 04:57:54 发布

您现在位置:Python中文网/ 问答频道 /正文

我想要一个在Windows上支持Unicode的shell。PowerShell的船上似乎没有。在

PowerShell V2(Windows 7 x64):

PS C:\> powershell
Windows PowerShell
Copyright (C) 2009 Microsoft Corporation. All rights reserved.

PS C:\> python
Python 2.6.2 (r262:71605, Apr 14 2009, 22:46:50) [MSC v.1500 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> unicode_char=unichr(0xf12)
>>> unicode_char
u'\u0f12'
>>> print unicode_char
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "C:\python26\lib\encodings\cp437.py", line 12, in encode
    return codecs.charmap_encode(input,errors,encoding_map)
UnicodeEncodeError: 'charmap' codec can't encode character u'\u0f12' in position 0: character maps to <undefined>
>>>

我用PowerShell ISE得到了类似的结果,尽管网络上有些地方声称它支持Unicode或其他。。。在

Python集成开发环境(IDLE)2.6.2似乎运行良好:

^{pr2}$

空闲很慢,我更喜欢另一个外壳,有什么想法吗?我能在PowerShell中实现这个功能吗?在


Tags: inwindowslineunicodeshellv2encodefile
3条回答

PowerShell本身是Unicode,但是经典控制台在Unicode方面有问题。但PowerShell ISE绝对是Unicode。在

试试这个:

PS C:\> $a = [char]0xf12
PS C:\> echo $a

这是Python玩得不好。在

您可以在启动Python之前尝试chcp 65001(将代码页设置为UTF-8)。但是没有承诺(我没有试着看看它是否有效,这台机器上没有安装Python)。在

Windows控制台子系统不是Unicode,而是基于代码页的。您可以随意设置代码页:

PS> chcp 65001
PS> ipy64.exe
>>> print unichr(0x3a9)
Ω

我无法让(0xF12)用该代码页给出正确的字符。也许它可以在另一个代码页上找到。在

ISE可以显示Unicode并接受Unicode输入,例如

^{pr2}$

然而,ISE似乎不能很好地使用IronPython解释器。在

此外,ISE似乎通过标准输出处理来自本机应用程序的Unicode:

$src = @'
namespace Foo {
    public class Bar
    {
        public static void Baz()
        {
            System.Console.Out.WriteLine("\u0f12");
            System.Console.Out.WriteLine("\u00e4");
            System.Console.Out.WriteLine("\u03a9");
        }
    }
}
'@

Add-Type -TypeDefinition $src

[Foo.Bar]::Baz()
༒
ä
Ω

请注意,在Windows控制台中发出chcp 65001时,字体必须是Lucida控制台,而不是任何位图字体。理论上,其他等距的ttf字体应该可以工作,但实际上不行。这是一个属性问题,MS检查用于控制台的等宽字体,Lucida console包括这些属性。在

对于Python Windows控制台unicode问题here,至少有一个问题尚未解决。在

相关问题 更多 >