简体   繁体   English

Python 2.7.6-Sublime 2中的UnicodeEncodeError,但终端中没有错误

[英]Python 2.7.6 - UnicodeEncodeError in Sublime 2 but NO error in Terminal

I have a script that reads from a website. 我有一个从网站读取的脚本。 The website has thai characters. 该网站有泰语字符。

When I run the script in the Terminal, it prints the text fine. 当我在终端中运行脚本时,它可以很好地打印文本。

When I run the script in the Sublime 2 (cmd+B) I get: 当我在Sublime 2(cmd + B)中运行脚本时,我得到:

UnicodeEncodeError: 'ascii' codec can't encode characters in position 1-7: ordinal not in range(128)

I have googled and read but nothing seems to work. 我已经用谷歌搜索和阅读,但是似乎没有任何作用。 Any tips? 有小费吗?

The Sublime Text 2 command window apparently encodes Unicode strings as ascii before outputting them if they don't have an encoding attached. 如果没有附加编码,那么Sublime Text 2命令窗口显然会在输出之前将Unicode字符串编码为ascii

Test case that runs in Terminal, but fails to run under Sublime Cmd+B: 在终端中运行但无法在Sublime Cmd + B下运行的测试用例:

# -*- coding: utf-8 -*-
print u'Hello 漢字!'

Encoding the unicode object when printing it works around this for me: 在打印时对unicode对象进行编码对我来说可以解决此问题:

# -*- coding: utf-8 -*-
print u'Hello 漢字!'.encode('utf-8')

Try File-> Save With Encoding -> UTF-8 and run it again. 尝试“文件”->“使用编码保存”-> UTF-8,然后再次运行。 This should work. 这应该工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM