Skip to Content
Bad Data 技術手冊
book

Bad Data 技術手冊

by Q. Ethan McCallum
September 2013
Intermediate to advanced
264 pages
5h
Chinese
GoTop Information, Inc.
Content preview from Bad Data 技術手冊
問題:應用程式限定的字元洩漏至純文字檔案中
|
63
... encoding='macroman') as f:
print(f.readline())
0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnop
qrstuvwxyz
ͣμͦ
ÀÁÂÃÄÅÆÇÈÉÊËÌÍÎÏÑÒÓÔÕÖØÙÚÛÜßàáâãäåæçè
éêëìíîïñòóôõöøùúûüÿı
Œœ
Ÿƒ
範例 4-7 的第一個範例單純的呈現出,已知為 MacRoman 編碼的文字並無法在 Python
中正確被顯示。在第二個範例中,我們使用
str.decode
來產生正確的 Unicode 字串,
這樣就可以正確顯示了。在範例 4-7 中的最後一個範例則是在讀取檔案時使用
codecs.
open
codecs.open
被呼叫的方式就如同普通的
open
函式一樣,並且會回傳一個行為如
file
的物件,唯一的不同點在於,它在讀取時會自動的將位元組字串解碼為 Unicode
以及會在寫入時自動的將 Unicode 編碼為指定的編碼方式。在範例 4-7 的最後一個範例
中,對
f.readline
的呼叫會回傳一個適當被解碼的 Unicode 字串。對
print
的呼叫能夠正
確的處理 Unicode 字串。
截至目前為止,我們已經討論了,擁有未知或是錯誤的字元編碼的文字是如何能在文
字處理應用程式中變成壞資料。使用文字當成鍵的連接(join)動作也許會因為使用不
同的文字編碼方法而造成行為不如預期。我們已經看過使用 ...
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

RxJava反应式编程

RxJava反应式编程

Tomasz Nurkiewicz, Ben Christensen
高效能PYTHON程式設計

高效能PYTHON程式設計

Micha Gorelick, Ian Ozsvald
Kubernetes编程

Kubernetes编程

Michael Hausenblas, Stefan Schimanski

Publisher Resources

ISBN: 9789862768952