简体   繁体   English

VScode的Python调试控制台无法正确打印Unicode中文字符

[英]VScode's Python Debug Console doesn't print Unicode Chinese Charactor Properly

Image to show the problem Here is the code to illustrate the problem: 显示问题的图像这里是说明问题的代码:

# -*- coding:utf-8 -*-
text = u"严"
print text

If I run the code above in VSCode debug, it will prints "涓" instead of "严", which is the result of the first 2 byte (\\xe4\\xb8) of u"严" in UTF-8 (\\xe4\\xb8\\xa5), decoded in gbk codec. 如果我在VSCode调试中运行上面的代码,它将打印“涓”而不是“严”,这是UTF-8(\\ xe4 \\)中u“严”的前2个字节(\\ xe4 \\ xb8)的结果xb8 \\ xa5),以gbk编解码器解码。 \\xe4\\xb8 in gbk is "涓". gbk中的\\ xe4 \\ xb8是“涓”。

However if I run the same code in pycharm it prints "严" exactly as I expected. 但是,如果我在pycharm中运行相同的代码,它将完全按照我的预期打印“严”。 And it is the same If I run the code in powershell. 如果我在Powershell中运行代码,也是如此。

Wired the VSCode python debugger behaves different with python interpreter. 有线的VSCode python调试器与python解释器的行为不同。 How can I get the print result correct, I do not think add a decode("gbk") in the end of every text would be a good idea. 我如何才能正确获得打印结果,我不认为在每个文本的末尾添加解码(“ gbk”)是一个好主意。

My Environment data 我的环境数据

  • VS Code version: 1.21 VS Code版本:1.21
  • VSCode Python Extension version : 2018.2.1 VSCode Python扩展版本:2018.2.1
  • OS and version: Windows 10 操作系统和版本:Windows 10
  • Python version : 2.7.14 Python版本:2.7.14
  • Type of virtual environment used : No 使用的虚拟环境类型:否
  1. For Windows users, in your System Variables, add PYTHONIOENCODING Variables,change its value to UTF-8 , then restart vscode, this worked on my pc. 对于Windows用户,在系统变量中添加PYTHONIOENCODING变量,将其值更改为UTF-8 ,然后重新启动vscode,这在我的PC上有效。

  2. Modify task.json file in vscode, I am not sure if it will still work on version 2.0. 修改task.json文件,我不确定它是否仍适用于2.0版。 You can find it here: Changing the encoding for a task output or here in github: Tasks should support specifying the output encoding 您可以在这里找到它: 更改任务输出的编码或在github中找到: 任务应支持指定输出编码

  3. add this before you start a py script: import io import sys sys.stdout = io.TextIOWrapper(sys.stdout.buffer,encoding='utf8') 在启动py脚本之前添加以下内容: import io import sys sys.stdout = io.TextIOWrapper(sys.stdout.buffer,encoding='utf8')

If you open your python file in VS2017 you can do the following: 如果您在VS2017中打开python文件,则可以执行以下操作:

Go to: 去:

  1. File-> 文件->
  2. Save selected item as -> 将所选项目另存为->
  3. click on the down-arrow next to "Save button" 点击“保存按钮”旁边的向下箭头

  4. clicking "Save With Encoding... 点击“使用编码保存...”

  5. select the type of coding you need... 选择您需要的编码类型...
  6. if .py already saved then overwrite file > select "yes" 如果.py已经保存,则覆盖文件>选择“是”

select for example : "Chinese Simplified (GB18030) - Codepage 54936" 例如,选择"Chinese Simplified (GB18030) - Codepage 54936"

Also, add the following on line 2 of your .py file: 另外,在.py文件的第2行中添加以下内容:

# -*- coding: gb18030 -*- or # -*- coding: gb2312 -*- # -*- coding: gb18030 -*-# -*- coding: gb2312 -*-

Those encodings accept your 严 character. 这些编码接受您的字符。

Nice link to endocoder/decoder tester here . 此处链接到内码/解码器测试仪

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM