简体   繁体   中英

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:

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.

Test case that runs in Terminal, but fails to run under Sublime Cmd+B:

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

Encoding the unicode object when printing it works around this for me:

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

Try File-> Save With Encoding -> UTF-8 and run it again. This should work.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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