【问题标题】:Task output encoding in VSCodeVSCode 中的任务输出编码
【发布时间】:2017-11-27 05:05:59
【问题描述】:

我正在使用 Visual Studio Code 学习 BeautifullSoup,当我运行此脚本时:

import requests
from bs4 import BeautifulSoup
from fake_useragent import UserAgent

ua = UserAgent()
header = {'user-agent':ua.chrome}
google_page = requests.get('https://www.google.com',headers=header)

soup = BeautifulSoup(google_page.content,'lxml') # html.parser

print(soup.prettify())

我收到以下错误:

Traceback(最近一次调用最后一次):文件 “c:\ ... \intro-to-soup-2.py”,第 13 行,在 打印(soup.prettify())文件“C:\ ... \Local\Programs\Python\Python36-32\lib\encodings\cp1252.py”, 第 19 行,在编码中 return codecs.charmap_encode(input,self.errors,encoding_table)[0] UnicodeEncodeError: 'charmap' codec can't encode character '\U0001f440' 在位置 515:字符映射到

如果我在汤变量中强制编码 utf-8,我将无法使用 prettify,因为它不适用于字符串... 还尝试在第一行代码上使用 # -- coding: utf-8 -- 没有成功。

这是我的这个项目的 tasks.json:

{
// See https://go.microsoft.com/fwlink/?LinkId=733558
// for the documentation about the tasks.json format
"version": "0.1.0",
"command": "python",
"isShellCommand": true,
"args": ["${file}"],
"files.encoding": "utf8",
// Controls after how many characters the editor will wrap to the next line. Setting this to 0 turns on viewport width wrapping (word wrapping). Setting this to -1 forces the editor to never wrap.
"editor.wrappingColumn": 0, // default value is 300
// Controls the font family.
"editor.fontFamily": "Consolas, 'Malgun Gothic', '맑은 고딕','Courier New', monospace",
// Controls the font size.
"editor.fontSize": 15,
"showOutput": "always"
}

完全相同的代码在 PyCharm 中运行,没有任何问题。 有什么想法可以在 Visual Studio Code 中解决这个问题吗?

这是我的“点冻结”结果:

astroid==1.5.3
beautifulsoup4==4.5.3
colorama==0.3.9
fake-useragent==0.1.7
html5lib==0.999999999
isort==4.2.15
lazy-object-proxy==1.3.1
lxml==3.7.2
mccabe==0.6.1
pylint==1.7.1
requests==2.12.5
selenium==3.4.3
six==1.10.0
webencodings==0.5
wrapt==1.10.10
xlrd==1.0.0
XlsxWriter==0.9.6

感谢您的宝贵时间,

尤尼托。

【问题讨论】:

  • pycharm 和 VScode 是否运行相同的 python 安装?
  • 如何查看我在 VSCode 中使用的版本?我今天安装了 PyCharm,所以我认为它使用的是最新的
  • @Eunito 在 VSCode 中将其作为脚本运行:import sys; print('Python %s on %s' % (sys.version, sys.platform))。此外,pycharm 可能正在使用您拥有的任何安装(如果您有多个安装),因此最近下载它并不能保证它运行最新的 python。
  • 在 pycharm 和 VSC 上运行,输出为 Python 3.6.0 (v3.6.0:41df79263a11, Dec 23 2016, 07:18:10) [MSC v.1900 32 bit (Intel)]在win32上
  • edit - 刚刚将python更新到最新版本,问题不断发生:(

标签: python visual-studio-code vscode-settings vscode-tasks


【解决方案1】:

这里的问题似乎是 python 解释器认为 stdout/stderr 支持的编码。出于某种原因(可以说是 VSCode 中的一个错误),它被设置为一些特定于平台的值(Windows 中的 cp1252,我能够在 OS X 上重现该问题并获得 ascii)而不是 VSCode 输出的 utf-8窗口支持。你可以修改你的 task.json 看起来像这样来解决这个问题——它设置了一个环境变量,强制 Python 解释器使用 utf8 进行输出。

{
    // See https://go.microsoft.com/fwlink/?LinkId=733558
    // for the documentation about the tasks.json format
    "version": "0.1.0",
    "command": "python3",
    "isShellCommand": true,
    "args": ["${file}"],
    "showOutput": "always",
    "options": {
        "env": {
            "PYTHONIOENCODING":"utf-8"
        }
    }
}

相关位是“选项”字典。

【讨论】:

  • 你先生是神!谢谢!
  • 如何设置cpp编码?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-02-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多