python多平台编码
高洛峰
高洛峰 2017-04-17 16:30:10
0
5
406

最近遇到一个几乎每个人会遇到的事情,就是编码的问题。
网上很多事使用

“# 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\''

但是还是不能解决不同的编辑器下的编码问题。请问这样的问题怎么解决呢?

高洛峰
高洛峰

拥有18年软件开发和IT教学经验。曾任多家上市公司技术总监、架构师、项目经理、高级软件工程师等职务。 网络人气名人讲师,...

reply all(5)
小葫芦

For reference on this coding issue, you can look for it on Zhihu, where the coding instructions are pretty good.

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:


>>>'知乎'
>>>'\xd6\xaa\xba\xf5'
>>>u'\u77e5\u4e4e'.encode('gbk')
>>>'\xd6\xaa\xba\xf5'
>>>u'\u77e5\u4e4e'.encode('utf-8')
>>>'\xe7\x9f\xa5\xe4\xb9\x8e'

So you see, the above example shows that when you enter a string in a terminal, the terminal has actually used its default encoding method to process the string for you (shown as an encoded string). So after you use the decode('gbk') method to process the string at this time, it will become a unicode character encoding. The unicode encoding method is the basis for other encoding methods.

Of course, in the case of Linux, the questioner can try again.

Of course, I have said so many things that the subject may already know. I just want to explain my own approach. If some files are often processed by one or several terminals, convert the things in the original terminal into unicode characters. string, and then convert it into the encoding required by another platform. If it is just a terminal for two platforms, just change GBK and UTF-8. It may be less efficient.

巴扎黑

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

Peter_Zhu

When writing Python on Windows-Chinese version, you must be prepared to struggle with coding

My experience is here http://segmentfault.com/a/1190000004018799

Peter_Zhu

Try this

import sys
reload(sys)
sys.setdefaultencoding('utf8')
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!