Home  >  Article  >  Backend Development  >  Python vscode outputs garbled characters

Python vscode outputs garbled characters

angryTom
angryTomOriginal
2019-11-04 14:35:373995browse

Python vscode outputs garbled characters

python vscode outputs garbled characters

When using Visual Studio Code to write python code, will you encounter output? Garbled characters appear in Chinese, but no error is reported? Just like the picture below:

Python vscode outputs garbled characters

The solution is very simple, let’s take a look at it

1. Method 1

Add an encoding statement to the header of the python file

# -*-coding:utf-8 -*-
import io
import sys
#改变标准输出的默认编码
sys.stdout=io.TextIOWrapper(sys.stdout.buffer,encoding='utf8')

2. Method 2

Add an encoding statement to the environment variable Variable setting: PYTHONIOENCODING=utf-8

Python vscode outputs garbled characters

3. Method 3

Specify the use of UTF by setting compilation parameters -8 encoding: "python": "set PYTHONIOENCODING=utf8 && python"

Python vscode outputs garbled characters

Recommendation: "python tutorial"

The above is the detailed content of Python vscode outputs garbled characters. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn