Mac Sublime compiled python unicodeencodeerror: ' ASCII ' codec can ' t encode characters in position 6-8: Ordinal not in range (128)

Source: Internet
Author: User

Just learned Python, want to print a "Hello Zhanglin", the code is as follows:

# !/usr/bin/env Python3 # -*-coding:utf-8-*- Print ('hello Zhanglin ')

Use sublime to run a bit, unexpectedly error???

Traceback (most recent): File"/users/zhanglinfeng/documents/python/\u7ec3\u4e60/\u5b57\u7b26\u4e32\u7f16\u7801\u53ca\u8f93\u51fa\u683c\ u5f0f.py", Line 4,inch<module>Print('Hello \u5f20\u6797\u5cf0') Unicodeencodeerror:'ASCII'Codec can'T encode characters in position 6-8: Ordinal not in range (+)[Finishedinch0.1s with exit code 1][cmd: ['/library/frameworks/python.framework/versions/3.6/bin/python3','- u','/users/zhanglinfeng/documents/python/Practice/String encoding and output format. PY']][dir:/users/zhanglinfeng/documents/python/practice] [Path:/usr/bin:/bin:/usr/sbin:/sbin]

Do you learn a new language and the first program has to be "Hello Word"? Try it, special Hello Word is really right. Is it a Chinese question? But I clearly wrote the # -*-coding:utf-8-*- . Go to terminal run under this PY file, special terminal unexpectedly right, can print Hello Zhanglin. What's the situation?

Online search, yo Ah, reported this wrong people are quite many ah. There are probably several answers to these questions:

1. Because the default encoding is ASCII, you need to set your own Python's default encoding to UTF8 encoding format

Query system default encoding you can enter the following command in the interpreter:

>>>import  sys   >>>sys.getdefaultencoding ()  

To set the default encoding, use:

>>>sys.setdefaultencoding ('UTF8'

However, my Python default encoding is UTF8, so the answer is pass.

2. Limitations that are considered to be print

You need to add the following code to the front

Import io   Import SYS    = io. Textiowrapper (sys.stdout.buffer,encoding='UTF8'# Change the default encoding for standard output 

Mine is the second answer to solve.

Mac Sublime compiled python unicodeencodeerror: ' ASCII ' codec can ' t encode characters in position 6-8: Ordinal not in range (128)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.