Marquez.Z
Marquez.Z

Reputation: 411

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

Image to show the problemHere 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. \xe4\xb8 in gbk is "涓".

However if I run the same code in pycharm it prints "严" exactly as I expected. And it is the same If I run the code in powershell.

Wired the VSCode python debugger behaves different with python interpreter. 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.

My Environment data

Upvotes: 3

Views: 3330

Answers (2)

Jay
Jay

Reputation: 824

  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.

  2. Modify task.json file in vscode, I am not sure if it will still work on version 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

  3. add this before you start a py script: import io import sys sys.stdout = io.TextIOWrapper(sys.stdout.buffer,encoding='utf8')

Upvotes: 3

ZF007
ZF007

Reputation: 3741

If you open your python file in VS2017 you can do the following:

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"

select for example : "Chinese Simplified (GB18030) - Codepage 54936"

Also, add the following on line 2 of your .py file:

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

Those encodings accept your 严 character.

Nice link to endocoder/decoder tester here.

Upvotes: -1

Related Questions