zoukankan      html  css  js  c++  java
  • Python2 中文编码处理

    今天写了几个脚本,都遇到了中英文混编的情况。需求要将其中的中文标点符号切换为英文符号。
    举个例子:

    tags = '你好,good, 国语'


    要将其中的中文半角逗号替换为英文逗号,为了方便后续的处理
    如下处理:

    tags = tags.replace(',', ',')

    会抛出如下异常:
    UnicodeDecodeError: 'ascii' codec can't decode byte ...


    python中字串分成两种,byte string 和unicode string
    一般来说,设定好#coding=utf-8后,所有带中文的参数都会声明成utf-8编码的byte string
    但是在函数中产生的字串则是unicode string

    byte string 和 unicode string不能混用,所以就会抛出UnicodeDecodeError异常

    byte_str = 'hello, this is byte string'
    unicode_str = u'hello, this is unicode string'


    所以有三种解决方案:
    1. 全都转为byte string
    2. 全都转为unicode string
    3. 设置系统编码

    1. 全都转为byte string

    '你好' + request.forms.tags.encode('utf-8')


    2. 全都转unicode.string

    u'你好' + request.forms.tags


    byte string 和unicode string相互转换

    b_s = 'test'
    u_s = unicode(b_si, 'utf-8')
    back_to_b_s = u_s.encode('utf-8')


    3. 设置系统默认编码

    import sys
    reload(sys)
    sys.setdefaultencoding('utf-8')

    这样就可以任意的使用了


    所以上面的问题就有解了:

    tags = tags.replace(unicode(',','utf-8'), ',')

    或者

    tags = tags.encode('utf-8').replace(',', ',')

    或者
    调用setdefaultencoding设置系统encoding了


    此外,还有读取UTF-8文件
    可以使用codecs模块

    import codecs
    handler = codecs.open('test', 'r', 'utf-8')
    u = handler.read()  # returns a unicode string from the UTF-8 bytes in the file

    codesc还能将传给write的unicode string转换为任何编码


    在编写代码过程中,变量必须是ascii编码的,为了可以在文件中写中文,python需要知道文件不是ASCII编码

    #!/usr/bin/env python

    下添加

    # -*- coding: utf-8 -*-


    以上在python2中有效,在python3中已经区分了unicode string 和byte string,并且默认编码不再是ASCII

    参考资料
    http://www.evanjones.ca/python-utf8.html

  • 相关阅读:
    母版页中对控件ID的处理
    使用Gridview绑定数据库中的图片
    导出Excel表格时,如何把数据库表中的编号转换成配置文件中的"汉字"
    ORA01502: 索引'P_ABCD.PK_WEB_BASE'或这类索引的分区处于不可用状态
    Oracle 把触发器说透
    规模估算失准 软件开发成空中楼阁
    在web开发中的三个层次使用事务
    oninput,onpropertychange,onchange的用法和区别
    Oracle 把游标说透
    在datatable中,在指定位置插入列
  • 原文地址:https://www.cnblogs.com/jiangu66/p/3186881.html
Copyright © 2011-2022 走看看