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