使用 Sublime Text 3 在 Python 3 中打印 UTF-8 [英] printing UTF-8 in Python 3 using Sublime Text 3

查看:23
本文介绍了使用 Sublime Text 3 在 Python 3 中打印 UTF-8的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个 Python3 代码来尝试从 utf-8 编码的文件中读取和打印:

I have this Python3 code to attempt to read and print from a utf-8 encoded file:

f = open('mybook.txt', encoding='utf-8')
for line in f:
    print(line)

当我使用 Sublime Text 3 构建时,出现以下错误:

When I build using Sublime Text 3 I get the following error:

UnicodeEncodeError: 'ascii' codec can't encode character 'u2019' in position 18: ordinal not in range(128)

但是,当我使用 python3 在终端中执行我的代码时,它可以工作.

However, it works file when I just execute my code in the terminal with python3.

我的构建配置是

{
"cmd": ["/usr/local/bin/python3", "$file"]
, "selector": "source.python"
, "file_regex": "file "(...*?)", line ([0-9]+)"
}

如果我把它改成:

f = open('mybook.txt', encoding='utf-8')
for line in f:
    print(line.encode('utf-8'))

然后它会打印 utf-8 编码的字节字符串(我认为这就是正在发生的事情).

Then it does print the utf-8 encoded byte string (I think that's what's happening).

b'Hello
'
b'xc2xabxe2x80xa2
'
b'Goodbye'

但我也不知道如何在屏幕上打印 unicode 字符...

But I also don't know how to go from this to printing the unicode characters on the screen...

此外,如果我尝试按照 一个 python 程序无法在 sublime text 3 中执行,但在 bash 中成功 它仍然没有修复它.

Also, if I try changing this env variable as per A python program fails to execute in sublime text 3, but success in bash it still doesn't fix it.

推荐答案

答案实际上在您问题中链接的问题中 - PYTHONIOENCODING 需要设置为 "utf-8".但是,由于 OS X 很愚蠢并且不接受在终端或通过 .bashrc 或类似文件设置的环境变量,因此这不会以其他问题的答案中指示的方式工作.相反,您需要将该环境变量传递给 Sublime.

The answer was actually in the question linked in your question - PYTHONIOENCODING needs to be set to "utf-8". However, since OS X is silly and doesn't pick up on environment variables set in Terminal or via .bashrc or similar files, this won't work in the way indicated in the answer to the other question. Instead, you need to pass that environment variable to Sublime.

幸运的是,ST3 构建系统(我不了解 ST2)具有 "env" 选项.这是传递给 exec.py 的键和值字典,它负责在没有 "target" 选项集的情况下运行构建系统.正如我们在上面的评论中所讨论的那样,我指出您的示例程序在 Linux 上使用 ST3 (Build 3122) 运行时在包含非 ASCII 字符的 UTF-8 编码文本文件上运行良好,但在 OS X 上运行的版本不同. 让它运行所需要的只是改变构建系统以包含这一行:

Luckily, ST3 build systems (I don't know about ST2) have the "env" option. This is a dictionary of keys and values passed to exec.py, which is responsible for running build systems without the "target" option set. As discussed in our comments above, I indicated that your sample program worked fine on a UTF-8-encoded text file containing non-ASCII characters when run with ST3 (Build 3122) on Linux, but not with the same version run on OS X. All that was necessary to get it to run was to change the build system to enclude this line:

"env": {"PYTHONIOENCODING": "utf8"},

我保存了构建系统,点击B,程序运行良好.

I saved the build system, hit B, and the program ran fine.

顺便说一句,如果您想阅读 exec.pyPackages/Python/Python.sublime-build,或任何其他打包在 exec.py 中的文件code>.sublime-package 存档,通过 Package 安装 PackageResourceViewer控制.使用命令面板中的打开资源"选项来选择单个文件,或提取包"(两者都以PackageResourceViewer:"开头,或 prv 使用模糊搜索)将整个包提取到您的 Packages 文件夹,通过选择 Sublime Text → Preferences → Browse Packages...(在其他操作系统上只是 Preferences → Browse Packages...).它位于您硬盘上的以下位置:

BTW, if you'd like to read exec.py, or Packages/Python/Python.sublime-build, or any other file packed up in a .sublime-package archive, install PackageResourceViewer via Package Control. Use the "Open Resource" option in the Command Palette to pick individual files, or "Extract Package" (both are preceded by "PackageResourceViewer:", or prv using fuzzy search) to extract an entire package to your Packages folder, which is accessed by selecting Sublime Text → Preferences → Browse Packages… (just Preferences → Browse Packages… on other operating systems). It is located on your hard drive in the following location:

  • Linux:~/.config/sublime-text-3/Packages
  • OS X:~/Library/Application Support/Sublime Text 3/Packages
  • Windows 常规安装:C:UsersYourUserNameAppDataRoamingSublime Text 3Packages
  • Windows 便携式安装:InstallationFolderSublime Text 3DataPackages

一旦文件保存到您的 Packages 文件夹(如果您只是通过打开资源"选项查看它们并关闭而不更改或保存它们,它们将不会),它们将覆盖.sublime-package 存档中包含的同名文件.因此,例如,如果您想编辑 Python 包中的默认 Python.sublime-build 文件,您的更改将保存为 Packages/Python/Python.sublime-build,当你从菜单中选择 Python 构建系统时,它只会使用你的版本.

Once files are saved to your Packages folder (if you just view them via the "Open Resource" option and close without changing or saving them, they won't be), they will override the identically-named file contained within the .sublime-package archive. So, for instance, if you want to edit the default Python.sublime-build file in the Python package, your changes will be saved as Packages/Python/Python.sublime-build, and when you choose the Python build system from the menu, it will only use your version.

这篇关于使用 Sublime Text 3 在 Python 3 中打印 UTF-8的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆