VScode中文输出乱码处理方法(汇总)

VScode 中文乱码python版本

  • 在使用vscode的过程中,发现中文输出乱码;(文件乱码采用右下角的编译器就行)
  • 一番折腾后,汇总全部的方法

一、 利用系统的自带utf-8 支持 (不推荐)

解决方法:开始->设置->时间和语言->其他日期、时间和区域设置->区域.更改位置->管理.更改系统区域设置->勾选->重启
VScode中文输出乱码处理方法(汇总)
目前已知问题,软件会乱码,安装和卸载中文软件,例如有道词典查词也会乱码,

二、临时处理方法

chcp命令
终端输入:chcp 65001
缺点,重开需要重新输入
此外通过打开“文件”–“首选项”–“用户设置”,然后在setting.json中设置(没有成功):

{
“editor.fontSize”: 18,
“terminal.integrated.shellArgs.windows”: ["/K chcp 65001 >nul"],
“terminal.integrated.fontFamily”: “Lucida Console”,
}
VScode中文输出乱码处理方法(汇总)

三、修改系统变量 (推荐其中第二种)

系统环境–新建–输入变量名: PYTHONIOENCODING ,变量值为utf-8,点击确定
VScode中文输出乱码处理方法(汇总)
或者 (推荐)
“code-runner.executorMap”: {
“python”: “set PYTHONIOENCODING=utf8 && python”
},
VScode中文输出乱码处理方法(汇总)
debug下需要额外处理
菜单Debug->Open Configurations,打开launch.json
VScode中文输出乱码处理方法(汇总)
增加如下代码
“env”:{
“PYTHONIOENCODING”:“gbk”
}

四、修改文件输出

--coding:utf-8 --

import io
import sys
#改变标准输出的默认编码
sys.stdout=io.TextIOWrapper(sys.stdout.buffer,encoding=‘utf8’)
VScode中文输出乱码处理方法(汇总)