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)