Skip to Content
Python 技術手冊 第三版
book

Python 技術手冊 第三版

by lex Martelli, Anna Martelli Ravenscroft, Steve Holden
January 2018
Intermediate to advanced content levelIntermediate to advanced
856 pages
17h 17m
Chinese
GoTop Information, Inc.
Content preview from Python 技術手冊 第三版
Unicode
|
287
Unicode
要將 bytestrings(位元組字串)轉為 Unicode 字串,在 v2 中,就使用
unicode
內建函式,或 bytestrings
decode
方法,又或者,你可以讓轉換
隱含地發生,在你傳入一個 bytestring 到預期 Unicode 的函式之時。在 v3
中,這種轉換永遠都必須是明確的,以 bytestrings
decode
方法進行。
在上述任一種情況中,轉換都是透過一種叫做
codec
co
der-
dec
oder
縮寫,即「編碼解碼器」)的輔助物件來進行的。一個 codec 也可以把
Unicode 字串轉為 bytestrings,不管是明確地以 Unicode 字串的
encode
法進行,或是,僅限於 v2,隱含地進行。
要識別一個 codec,就將 codec 的名稱傳入
unicode
decode
encode
。當
你沒有傳入 codec 名稱,或是在 v2 中隱含轉換時,Python 就會使用預設的
編碼(encoding),一般是 v2 中的
'ascii'
v3 中的
'utf8'
)。
每個轉換都有一個
errors
參數,它是指示要如何處理轉換錯誤的一個字
串。預設值是
'strict'
,代表任何的錯誤都會提出例外。如果
errors
'replace'
,在 bytestring 結果中,轉換會以
'?'
取代導致錯誤的每個錯
誤,在 Unicode 結果中則是以
u'\ufffd'
取代。當
errors
'ignore'
,轉
換會無聲無息地跳過導致錯誤的字元 ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

測試驅動開發|使用 Python

測試驅動開發|使用 Python

Harry J.W. Percival
Linux 内核观测技术BPF

Linux 内核观测技术BPF

David Calavera, Lorenzo Fontana

Publisher Resources

ISBN: 9789864766819