使用 Python 在 PowerShell 中使用 Unicode?Windows 中的替代外壳? [英] Unicode in PowerShell with Python? Alternative shells in Windows?

查看:27
本文介绍了使用 Python 在 PowerShell 中使用 Unicode?Windows 中的替代外壳?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

PowerShell V2 (Windows 7 x64):

PS C:\>电源外壳Windows PowerShell版权所有 (C) 2009 Microsoft Corporation.版权所有.PS C:\>PythonWin32 上的 Python 2.6.2(r262:71605,2009 年 4 月 14 日,22:46:50)[MSC v.1500 64 位 (AMD64)]输入帮助"、版权"、信用"或许可"以获取更多信息.>>>unicode_char=unichr(0xf12)>>>unicode_char你'\u0f12'>>>打印 unicode_char回溯(最近一次调用最后一次):文件<stdin>",第 1 行,在 <module> 中文件C:\python26\lib\encodings\cp437.py",第 12 行,编码返回 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 似乎工作正常:

<预><代码>>>>unicode_char=unichr(0xf12)>>>打印 unicode_char༒>>>

IDLE 很慢,我更喜欢另一个 shell,有什么想法吗?我可以在 PowerShell 中完成这项工作吗?

解决方案

Windows 控制台子系统不是 Unicode,而是基于代码页.您可以尝试设置代码页:

PS>CHCP 65001PS>ipy64.exe>>>打印 unichr(0x3a9)Ω

我无法让 (0xF12) 为该代码页提供正确的字符.也许它在另一个代码页上可用.

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

PS>[字符]0xf12༒PS>[字符]0xe4一种PS>[字符]0x3a9Ω

但是,ISE 似乎不能很好地与 IronPython 解释器配合使用.

进一步说明 ISE 似乎通过标准输出处理来自本机应用程序的 Unicode:

$src = @'命名空间 Foo {公共课吧{公共静态无效巴兹(){System.Console.Out.WriteLine("\u0f12");System.Console.Out.WriteLine("\u00e4");System.Console.Out.WriteLine("\u03a9");}}}'@添加类型 -TypeDefinition $src[Foo.Bar]::Baz()༒一种Ω

I want a shell that supports Unicode on Windows. PowerShell as it ships doesn't seem to.

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>
>>>

I get similar results with PowerShell ISE, even though some places around the web claim it to be Unicode supporting or whatever...

The Python Integrated Development Environment (IDLE) 2.6.2 seems to work fine:

>>> unicode_char=unichr(0xf12)
>>> print unicode_char
༒
>>> 

IDLE is very slow, and I would prefer another shell, any ideas? Can I make this work in PowerShell?

解决方案

The Windows console subsystem is not Unicode, but code page based. You can play around with setting the code page:

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

I couldn't get (0xF12) to give the right character with that codepage. Perhaps it is available on another code page.

ISE can display Unicode and accept Unicode input, for example,

PS> [char]0xf12
༒
PS> [char]0xe4
ä
PS> [char]0x3a9
Ω

However, ISE doesn't seem to play well with the IronPython interpreter.

Further to the point that ISE seems to handle Unicode coming from a native application via stdout:

$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()
༒
ä
Ω

这篇关于使用 Python 在 PowerShell 中使用 Unicode?Windows 中的替代外壳?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆