使用python使用chcp 65001预激活的启动控制台窗口 [英] Launch console window pre-activated with chcp 65001 using python

查看:495
本文介绍了使用python使用chcp 65001预激活的启动控制台窗口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用了一个Python库,该库将Unicode字符输出到Windows控制台。如果我在库中调用一个打印Unicode字符的函数,它将抛出一个异常'charmap'编解码器无法对字符进行编码

I use a python library that prints out a Unicode character to windows console. If I call a function on the library that prints out Unicode character, it will throw an exception 'charmap' codec can't encode characters.

这就是我试图解决该错误的方法:
使用 os.system( chcp 65001)从python调用 chcp 65001 Windows控制台命令,然后调用库函数。

So this is what I tried to solve that error: Call "chcp 65001" windows console command from python using os.system("chcp 65001") before calling the library function.

我知道有类似的问题,这就是为什么我尝试了上述解决方案。它成功地在控制台上调用了命令,并告诉我它已激活了代码页。

I know there are questions similar to this and that is why I tried the above solution. It successfully calls the command on the console and tells me that it activated the code page.

但是,异常再次出现。如果我尝试在不关闭前一个控制台的情况下再次运行该程序,则该程序将成功执行,没有任何异常。这意味着上面的控制台命令在第一次尝试后生效。

However, the exception showed up again. if I try to run the program again without closing the previous console, the program executes successfully without any exception. Which means the above console command takes effect after the first try.

我的问题是:有没有一种方法可以通过预先激活Unicode支持来启动Windows控制台,所以我不

My question is: is there a way to launch windows console by pre-activating Unicode support so that I don't have to call the program twice.

推荐答案

添加 / k chcp 65001 进入启动cmd窗口的快捷方式。或者,使用Python 3.6,该Python 3.6使用Windows Unicode API写入控制台,并忽略代码页。但是,您仍然需要打印内容的字体支持。

Add /k chcp 65001 to the shortcut launching the cmd window. Alternatively, use Python 3.6 which uses Windows Unicode APIs to write to the console and ignores the code page. You do still need font support for what you are printing, however.

这篇关于使用python使用chcp 65001预激活的启动控制台窗口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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