zoukankan      html  css  js  c++  java
  • 每天一点小进步(5):python编码问题

    # -*- coding: UTF-8 -*-:表示支持 UTF-8 中文。
    U‘中文’:表示 unicode 创建实例的格式显示中文。
    Encode(“utf-8”):表示以 UTF-8 编码对对象进行编码,获取 byte 类型对象。
    Decode(“utf-8”):表示以 UTF-8 编码对对象进行解码,获取字符串对象。

     注:decode时经常出现解码失败

    官方decode方法注解如下:

    def decode(self, *args, **kwargs): # real signature unknown
    """
    Decode the bytes using the codec registered for encoding.

    encoding
    The encoding with which to decode the bytes.
    errors
    The error handling scheme to use for the handling of decoding errors.
    The default is 'strict' meaning that decoding errors raise a
    UnicodeDecodeError. Other possible values are 'ignore' and 'replace'
    as well as any other name registered with codecs.register_error that
    can handle UnicodeDecodeErrors.
    """
    pass

    由于decode()方法的第二个参数默认是errors,严格(strict)形式,所有只要出现异常报就会造成UnicodeEdcodeErrors异常,但实际有些时候只需要获得想要的结果就行,不需要关注具体的UnicodeEdcodeErrors异常,将其更改为ignore或replace即可。

    改成如下写法:

    decode('utf8', 'ignore')

  • 相关阅读:
    struts2简介
    项目整合SpringDataRedis
    SpringDataRedis入门Demo
    包管理-rpm
    文件查找与压缩
    N042第一周
    Shell
    Linux下终端字体颜色设置方法
    文本处理工具作业
    正则表达式
  • 原文地址:https://www.cnblogs.com/wx2017/p/13042307.html
Copyright © 2011-2022 走看看