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')

  • 相关阅读:
    在jQuery中.bind() .live() .delegate() .on()的区别
    jquery小结测试题
    揭秘子类构造函数执行过程
    过滤器
    实现AJAX的基本步骤
    AJAX 原生态
    java工程师需要学什么
    Java进阶之路
    git入门大全
    轻松学JVM
  • 原文地址:https://www.cnblogs.com/wx2017/p/13042307.html
Copyright © 2011-2022 走看看