在Windows上设置Python终端编码 [英] Set Python terminal encoding on Windows

查看:302
本文介绍了在Windows上设置Python终端编码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我碰巧无法在Windows上的Python终端中设置字符编码。根据官方指南,这是一块蛋糕:

 # -  *  -  coding:utf-8  -  *  -  

好,现在测试:

 code> print'Русский'

产生一块mojibake。



PS IDE is Visual Studio 2010,if it matters

解决方案

更新:请参见 JF Sebastian的回答,以获得更好的解释和更好的解决方案。



# - * - coding:utf-8 - * - code>设置源文件的编码,而不是输出编码。



您必须在打印之前对该字符串进行编码,使用。在你的情况下,我猜你的代码页是西里尔文(cp866)。因此,

  print'Русский'.encode(cp866)
pre>

I happened to fail to set character encoding in Python terminal on Windows. According to official guide, it's a piece of cake:

# -*- coding: utf-8 -*-

Ok, now testing:

print 'Русский'

Produces piece of mojibake. What am doing wrong?

P.S. IDE is Visual Studio 2010, if it matters

解决方案

Update: See J.F. Sebastian's answer for a better explanation and a better solution.

# -*- coding: utf-8 -*- sets the source file's encoding, not the output encoding.

You have to encode the string just before printing it with the exact same encoding that your terminal is using. In your case, I'm guessing that your code page is Cyrillic (cp866). Therefore,

print 'Русский'.encode("cp866")

这篇关于在Windows上设置Python终端编码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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