Text的控制台中打印utf8字符串

Text的控制台中打印utf8字符串

本文介绍了使用Windows在Sublime Text的控制台中打印utf8字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Windows控制台cmd.exe(即Sublime Text的外部)中使用python myscript.py运行此代码时,它可以正常工作:

When running this code with python myscript.py from Windows console cmd.exe (i.e. outside of Sublime Text), it works:

# coding: utf8
import json
d = json.loads("""{"mykey": {"readme": "Café"}}""")
print d['mykey']['readme']

在使用在Sublime Text 2中运行它时,它将失败:

When running it inside Sublime Text 2 with , it fails:

  • 像这样(默认情况下):

  • Either like this (by default):

  • 或类似的方法,在应用中的解决方案/stackoverflow.com/questions/39576308/printing-utf-8-in-python-3-using-sublime-text-3>使用Sublime Text 3在Python 3中打印UTF-8 (即添加在构建系统中):

  • or like this, after applying the solution from this answer of printing UTF-8 in Python 3 using Sublime Text 3 (i.e. adding "env": {"PYTHONIOENCODING": "utf8"}, in the build system):

  • 在Python Sublime-build文件中添加"encoding": "utf-8"也无济于事

  • adding "encoding": "utf-8" in the Python Sublime-build file doesn't help either

    如果Sublime Text 2(适用于Windows)控制台包含一些UTF8字符,如何正确地print?

    How to print properly in Sublime Text 2 (for Windows) console, if it contains some UTF8 char?

    注意:这不是,我之前已经链接到这个问题.

    Note: this is not a duplicate of printing UTF-8 in Python 3 using Sublime Text 3, I already linked to this question before.

    这是Python.sublime-build文件:

    { "cmd": ["python", "-u", "$file"],
    "file_regex": "^[ ]*File \"(...*?)\", line ([0-9]*)",
    "selector": "source.python" }
    

    (我尝试使用和不使用"env": ...,使用和不使用"encoding": ...)

    (I tried with and without "env": ..., with and without "encoding": ...)

    推荐答案

    我发现了可能的解决方法:在Python.sublime-build文件中添加encoding参数:

    I have found a possible fix: add the encoding parameter in the Python.sublime-build file:

    {
    "cmd": ["python", "-u", "$file"],
    "file_regex": "^[ ]*File \"(...*?)\", line ([0-9]*)",
    "selector": "source.python",
    "encoding": "cp1252",
    ...
    

    注意:"encoding": "latin1"似乎也可以工作,但是-我不知道为什么-"encoding": "utf8"不能工作,即使.py文件是UTF8,即使Python 3使用UTF8等也是如此.

    Note: "encoding": "latin1" seems to work as well, but - I don't know why - "encoding": "utf8" does not work, even if the .py file is UTF8, even if Python 3 uses UTF8, etc. Mystery!

    现在可以使用:

    {
      "cmd": ["python", "-u", "$file"],
      "file_regex": "^[ ]*File \"(...*?)\", line ([0-9]*)",
      "selector": "source.python",
      "encoding": "utf8",
      "env": {"PYTHONIOENCODING": "utf-8", "LANG": "en_US.UTF-8"},
    }
    

    链接的主题:

    如何更改env技巧的MacOS Sublime Text 3中的首选编码.

    How to change the preferred encoding in Sublime Text 3 for MacOS for the env trick.

    这篇关于使用Windows在Sublime Text的控制台中打印utf8字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!