python版本3.5.1,print 輸出 ? 字符報錯,試了用decode轉(zhuǎn)換也沒用,求大神解決的辦法!
print("\xa9")
UnicodeEncodeError: 'gbk' codec can't encode character 'xa9' in position 0: ill
egal multibyte sequence
歡迎選擇我的課程,讓我們一起見證您的進(jìn)步~~
python3
It is normal to run under IDLE. The poster didn't tell you how you run it?
The output characters will be converted into the terminal encoding.
The default cmd under windows is cp936.
So when you run it directly, python will try to convert this character into gbk encoding, and this will happen after the conversion fails.
One solution is to execute your code in IDLE.
Method 2, first execute chcp 65001 in cmd to change the default encoding of cmd to unicode, and then execute python
http://imysqldba.blog.51cto.com/1222376/706672
# coding=utf-8
a = '?'
print a
Output result:
If you remove # coding=utf-8
in the code, the following exception will be thrown: # coding=utf-8
去掉,拋如下異常:
SyntaxError: Non-ASCII character '\xc2' in file D:\web\file_test.py on line 1, but no encoding declared; see http://python.org/dev/peps/pep-0263/ for details
然后我又寫成這樣:
# coding=utf-8
b = '\xa9'
print b
報錯了:
Decode error - output not utf-8
(這個錯是Sublime的問題,在命令行下未報錯,也什么都沒輸出)
雖然我用的是2.7
rrreee
Then I wrote like this:
2.7
and the exception is different from the subject, you can refer to it. ??Coding issues have always been one of the biggest headaches for programmers...??Maybe it’s a problem with the terminal. I use IDLE that comes with python 2.7.6, and no error is reported, but a box is output
This character is regarded as a gbk character. Are you sure the copyright symbol is written like this?
I specially installed 3.5.1 and tried it
Python 3.5.1 (default, Apr 22 2016, 09:00:13)
[GCC 4.8.4] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> print("\xa9")
?
There seems to be no problem here