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

Python 技術手冊 第三版

by lex Martelli, Anna Martelli Ravenscroft, Steve Holden
January 2018
Intermediate 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.

Read now

Unlock full access

More than 5,000 organizations count on O’Reilly

AirBnbBlueOriginElectronic ArtsHomeDepotNasdaqRakutenTata Consultancy Services

QuotationMarkO’Reilly covers everything we've got, with content to help us build a world-class technology community, upgrade the capabilities and competencies of our teams, and improve overall team performance as well as their engagement.
Julian F.
Head of Cybersecurity
QuotationMarkI wanted to learn C and C++, but it didn't click for me until I picked up an O'Reilly book. When I went on the O’Reilly platform, I was astonished to find all the books there, plus live events and sandboxes so you could play around with the technology.
Addison B.
Field Engineer
QuotationMarkI’ve been on the O’Reilly platform for more than eight years. I use a couple of learning platforms, but I'm on O'Reilly more than anybody else. When you're there, you start learning. I'm never disappointed.
Amir M.
Data Platform Tech Lead
QuotationMarkI'm always learning. So when I got on to O'Reilly, I was like a kid in a candy store. There are playlists. There are answers. There's on-demand training. It's worth its weight in gold, in terms of what it allows me to do.
Mark W.
Embedded Software Engineer

You might also like

利用 Python 进行数据分析(原书第2版)

利用 Python 进行数据分析(原书第2版)

Wes McKinney

Publisher Resources

ISBN: 9789864766819