使用sublime text 2和Python编码错误

问题描述 投票:0回答:2

在使用崇高文本2中的行丰富代码中的编码问题后,我决定进行更简单的实验。我使用以下代码,尝试用中文打印:

#!/usr/bin/env python
# -*- coding: utf-8 -*-

print('你好')

在sublime文本中运行时,我收到以下错误:

File "D:\xampp\htdocs\stam\chinese print try.py", line 4, in <module>
print('\u4f60\u597d')
File "d:\Python34\lib\encodings\cp1252.py", line 19, in encode
return codecs.charmap_encode(input,self.errors,encoding_table)[0]
UnicodeEncodeError: 'charmap' codec can't encode characters in position 0-1: character           maps to <undefined>
[Finished in 0.2s with exit code 1]

这是大而重要的。当我通过常规Python GUI运行代码时,一切都很好。它打印。

我试过看here,但不明白它是如何帮助我的(这可能确实是我在崇高的文本配置中缺乏理解)。

谢谢你的帮助。

python sublimetext2
2个回答
1
投票

与此printing UTF-8 in Python 3 using Sublime Text 3相似

您可以修改或添加新构建,并在构建文件中包含“env”:{“PYTHONIOENCODING”:“utf8”}。

例如,新的构建文件应如下所示(Windows 7):

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

0
投票

见:http://blog.sina.com.cn/s/blog_765abd7b0101dtbw.html

我做了这两件事,最终解决了这个问题:

  • 在代码中,addimport sys reload(sys) sys.setdefaultencoding("utf-8")
  • 在Python.sublime-build中,添加"encoding":"utf-8"
© www.soinside.com 2019 - 2024. All rights reserved.