zoukankan      html  css  js  c++  java
  • Python中文乱码的处理

    为什么Python使用过程中会出现各式各样的乱码问题,明明是中文字符却显示成“xe4xb8xadxe6x96x87”的形式?

    为什么会报错“UnicodeEncodeError: 'ascii' codec can't encode characters in position 0-1: ordinal not in range(128)”?本文就来研究一下这个问题。


    字符串在Python内部的表示是unicode编码,因此,在做编码转换时,通常需要以unicode作为中间编码,即先将其他编码的字符串解码(decode)成unicode,再从unicode编码(encode)成另一种编码。 

    decode的作用是将其他编码的字符串转换成unicode编码,如str1.decode('gb2312'),表示将gb2312编码的字符串str1转换成unicode编码。 

    encode的作用是将unicode编码转换成其他编码的字符串,如str2.encode('gb2312'),表示将unicode编码的字符串str2转换成gb2312编码。 

    因此,转码的时候一定要先搞明白,字符串str是什么编码,然后decode成unicode,然后再encode成其他编码


    代码中字符串的默认编码与代码文件本身的编码一致。 

    如:s='中文'

    如果是在utf8的文件中,该字符串就是utf8编码,如果是在gb2312的文件中,则其编码为gb2312。这种情况下,要进行编码转换,都需要先用decode方法将其转换成unicode编码,再使用encode方法将其转换成其他编码。通常,在没有指定特定的编码方式时,都是使用的系统默认编码创建的代码文件。 

    如果字符串是这样定义:s=u'中文'

    则该字符串的编码就被指定为unicode了,即python的内部编码,而与代码文件本身的编码无关。因此,对于这种情况做编码转换,只需要直接使用encode方法将其转换成指定编码即可。


    如果一个字符串已经是unicode了,再进行解码则将出错,因此通常要对其编码方式是否为unicode进行判断:

    isinstance(s, unicode)  #用来判断是否为unicode 

    用非unicode编码形式的str来encode会报错 


     如何获得系统的默认编码? 

    #!/usr/bin/env python
    #coding=utf-8
    import sys
    print sys.getdefaultencoding()  

    该段程序在英文WindowsXP上输出为:ascii 


    在某些IDE中,字符串的输出总是出现乱码,甚至错误,其实是由于IDE的结果输出控制台自身不能显示字符串的编码,而不是程序本身的问题。 

    如在UliPad中运行如下代码:

    s=u"中文"
    print s 

    会提示:UnicodeEncodeError: 'ascii' codec can't encode characters in position 0-1: ordinal not in range(128)。这是因为UliPad在英文WindowsXP上的控制台信息输出窗口是按照ascii编码输出的(英文系统的默认编码是ascii),而上面代码中的字符串是Unicode编码的,所以输出时产生了错误。

    将最后一句改为:print s.encode('gb2312')

    则能正确输出“中文”两个字。

    若最后一句改为:print s.encode('utf8')

    则输出:xe4xb8xadxe6x96x87,这是控制台信息输出窗口按照ascii编码输出utf8编码的字符串的结果。


    unicode(str,'gb2312')与str.decode('gb2312')是一样的,都是将gb2312编码的str转为unicode编码 

    使用str.__class__可以查看str的编码形式


    原理说了半天,最后来个包治百病的吧:)

    #!/usr/bin/env python 
    #coding=utf-8 
    s="中文" 
    
    if isinstance(s, unicode): 
    #s=u"中文" 
    print s.encode('gb2312') 
    else: 
    #s="中文" 
    print s.decode('utf-8').encode('gb2312')

    原文链接:http://www.jb51.net/article/17560.htm

  • 相关阅读:
    LVDS DP等显示器接口简介
    Eclipse智能感知及快捷键
    Error deploying web application directory D:apache-tomcat-8.0.26webappsservlet
    把tomcat默认的8080段口改成80端口
    警告: [SetPropertiesRule]{Server/Service/Engine/Host/Context} Setting property 'source' to 'org.eclipse.jst.jee.server did not find a matching property.
    软件危机
    SQL Server——热备份
    mysql热备份
    Oracle——热备份
    Nodejs基础
  • 原文地址:https://www.cnblogs.com/ooon/p/4766470.html
Copyright © 2011-2022 走看看