最近遇到一个几乎每个人会遇到的事情,就是编码的问题。
网上很多事使用
“# coding:utf-8 ”
的解决方案,但是这个方案我始终解决不了我的问题。
首先我使用上述的代码。但是在同个平台下不同的编辑器下,运行的结果也是不同的。
1)python自带ide的获取用户输入并转换成unicode时使用encoding('gb2312'),正常,但是使用encoding('UTF-8')时输入变成ASCII。
2)同样是获取用户输入并转换为unicode时,在cmd下运行时无论是提示字符串还是输入之后,就会报错,改变coding:gbk,提示正常,输入转换正常。
3)同样的功能,在PyCharm,必须使用UTF-8才能正常。
4)同样的功能,在linux,必须使用UTF-8编码才能正常运行。
之前,由于只实验过python自带的IDE和linux下的情况,使用了如下的解决方案:
IsSystems = sys.platform if IsSystems == "win32": return '\'gb2312\'' else: return '\'UTF-8\''
但是还是不能解决不同的编辑器下的编码问题。请问这样的问题怎么解决呢?
When using different terminals, let’s start with unicode. Take ‘Zhihu’ as an example. The unicode character encoding of ‘Zhihu’ is u’u77e5u4e4e’.
When you are on cmd on win, the situation is as follows:
Of course, in the case of Linux, the questioner can try again.
All files are saved as UTF-8
Writing in GBK is simply a hooligan
"#coding:utf-8" just tells the python interpreter what encoding method to use to run your program, but whether your program itself is encoded in utf-8 has nothing to do with this thing, you need to set it yourself. You can set it up as follows in pycharm
When writing Python on Windows-Chinese version, you must be prepared to struggle with coding
My experience is here http://segmentfault.com/a/1190000004018799
Try this