Setting the correct encoding when piping stdout in Python

后端 未结 10 2576
迷失自我
迷失自我 2020-11-22 01:21

When piping the output of a Python program, the Python interpreter gets confused about encoding and sets it to None. This means a program like this:

# -*- co         


        
10条回答
  •  被撕碎了的回忆
    2020-11-22 02:10

    On Windows, I had this problem very often when running a Python code from an editor (like Sublime Text), but not if running it from command-line.

    In this case, check your editor's parameters. In the case of SublimeText, this Python.sublime-build solved it:

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

提交回复
热议问题