python3.x - python输出特殊字符报错
天蓬老师
天蓬老师 2017-04-17 17:37:51
0
6
562

python版本3.5.1,print 输出 © 字符报错,试了用decode转换也没用,求大神解决的办法!

print("\xa9")

UnicodeEncodeError: 'gbk' codec can't encode character 'xa9' in position 0: ill
egal multibyte sequence

天蓬老师
天蓬老师

欢迎选择我的课程,让我们一起见证您的进步~~

reply all(6)
大家讲道理

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:

rrreee 🎜Error reported:🎜 rrreee 🎜Although I am using 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?

阿神

Try setting the environment variable language

伊谢尔伦

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

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template