Raymond has a question to do with the use of non-US alphabet characters.
When he first wrote programs with text in Spanish and ran them using Python,
he got an error--syntax error "non-ASCII character."
He did not get that error when running the same code in IDLE. It worked as it should.
He solved this by changing the encoding.
So as we're building a search engine that will scan pages in different languages
and perhaps use different language interfaces, how should we change our code
so that it does not run into problems with encoding? What should our default character set be?
Thanks for the question, Raymond. This is a really good point to bring up.
As you take inputs that include more languages than just English
the character sets are different.
If you look at a webpage, there's a header in the webpage
that identifies what characters set it's using.
This is part of the HTTP protocol to have a header that describes the content type,
which includes the character set.
The character set can be selected from a few different ones.
The most common one is called UTF-8.
That's using a way of encoding characters that makes the simple characters
that can be encoded in just 7 bits.
This is the character set that is known as ASCII.
These are characters that are common in English.
It doesn't cover all the characters that are used in all other languages,
but those 7-bit characters can be encoded using a small amount of space.
The standard we've been using in Python with standard strings is that they're all ASCII.
Each character is 1 byte.
There's only 255 possible characters that can be encoded in 1 byte though.
If you're wanting to deal with more languages, you need to use different characters.
The way to do that is called Unicode.
Unicode is a character set that can support large numbers of characters.
It's actually a way of encoding characters that doesn't have a limit on
how many characters you can support.
A way of encoding those that makes it efficient when the characters are small
but allows you to still encode a large number of characters is called UTF-8,
which is what most web browsers use.
To deal with this in Python, what you'd have to deal with instead of standard strings is Unicode strings.
There is a built-in type for Unicode characters.
You can convert a string to Unicode by using Unicode.
Then there are ways of encoding Unicode in other character sets.
If you wanted to build a web search engine that can deal with text that's not using
the standard English character set, you definitely need to worry about handling Unicode
and all these different character encodings.
レイモンドさんからは
アルファベット文字以外の文字を
使うことについての質問がありました
彼が最初にスペイン語のテキストでプログラムを
書いてPythonで実行したところ
ASCII文字ではないという構文エラーが出たそうです
IDLEで同じコードを実行した時にはそのエラーは出ず
正しく動いたとのことです
エンコーディングを変えたところ
エラーは解消されたそうです
そこで質問です
“私たちは検索エンジンを構築していますが
それが様々な言語のページを読んで”
“ひょっとすると様々な言語のインターフェースを
使うようにする場合に”
“エンコーディングの問題が起きないようにするには
コードをどのように変えるべきでしょうか?”
“デフォルトの文字セットは
どうするべきでしょうか?”
レイモンドさん
鋭い問題提起をありがとうございます
英語以外の言語を含む入力を受け取る場合は
文字セットが異なることに注意が必要です
Webページを見るとヘッダの中に
使われている文字セットが書かれています
これはHTTPプロトコルの一部となっていて
ヘッダには文字セットを含むコンテンツのタイプが
示されています
文字セットはいくつかの種類から選択されます
最も一般的なのがUTF-8と呼ばれるものです
UTF-8が使っているエンコーディングでは
基本的な文字は7ビットだけで表現できます
この文字セットはASCIIとして知られています
これは英語で一般的な文字です
他の言語で使われているすべての文字は
カバーしていませんが
小さなスペースで
7ビット文字のエンコードができます
私たちがPythonで使ってきた標準の文字列は
すべてASCIIです
各文字が1バイトです
1バイトでエンコードができるのは
255文字しかありません
したがってもっと多くの言語を扱いたい場合は
もっと多くの種類の文字を扱う必要があります
それを可能にするのがUnicodeです
Unicodeは多数の文字を利用可能にする
文字セットです
実質的には利用できる文字数が限定されない
エンコーディングだと言えるでしょう
Unicodeのエンコード方式のうち
コードポイントが小さい場合は効率的で
しかも多数の文字のエンコードを可能とするのが
UTF-8です
Unicodeは大抵のWebブラウザが使用しています
これをPythonで扱うためには
標準文字列ではなく
Unicode文字列を使う必要があります
Unicode文字の組み込み型がありますので
unicode関数を使用して
文字列をUnicodeに変換できます
そしてエンコーディングを
Unicodeから他の文字セットに変える方法があります
標準的な英語の文字セットを使っていないテキストを
扱えるような
検索エンジンを構築したい場合には
Unicodeや様々な文字のエンコーディングに
注意して対処する必要があります
Raymond 有個問題,處理使用非美國字母字元的問題
當他第一次以西班牙文寫程式時,使用了文字,
並使用 Python 來執行
他得到了一個 error —— 語法錯誤 "非 ASCII 字元"
在 IDE 執行相同的程式碼,並不會得到這個錯誤
它運作正常
他更改編碼 (encoding) ,以解決這個問題
所以,當我們創建搜尋引擎時,
它會掃描不同語言的頁面
可能使用了不同的語言介面,
我們應如何改變我們的程式碼
這樣,它就不會有編碼的問題?
我們預設的字元集應該是什麼?
謝謝你的問題,Raymond,談到這個觀點真的很好
當你採取的輸入,包括了多個語言,不僅是英語時
字元集是不同的
如果你看一個網頁,網頁有一個標題 (header)
標題識別了它使用的字元集
這是 HTTP 協定的一部分,
要有標題,描述內容的類型
包含字元集
字元集可以從一些不同的字元集中選擇
最普遍的一種稱為 UTF-8
它使用編碼字元的方式,可以使簡單的字元
以僅僅 7 個位元 (bit) 來編碼
這被稱為 ASCII 字元集
這些都是在英文中常見的字元
它不包括在其他語言中,使用的所有字元
但是,這些 7 位元的字元,能以少量的空間進行編碼
在 Python 裡的標準字串,其使用的標準都是 ASCII
每個字元是一個位元組 (byte)
僅有 255 個可能的字元,
可以在 1 個位元組 (byte) 做編碼
如果你想要處理更多的語言,你需要使用不同的字元
要做到這一點的方法稱為 Unicode
Unicode 是一種字元集,可以支援大量的字元
它實際上是一種編碼字元的方法
沒有限制你可以支援多少字元
有一種編碼方式,當字元少時,其效率高
但仍允許你編碼數量大的字元,稱為 UTF-8
這為大多數 web 瀏覽器所使用
為了在 Python 處理這個問題,
你必須使用 Unicode 字串,而不是標準字串
有一個內置類型的 Unicode 字元
您可以透過使用 Unicode ,將字串轉換為 Unicode
然後有其他字元集,有方法來編碼 Unicode
如果你想要建立一個搜尋引擎,
可以處理文字中,沒有使用標準的英文字元集
你一定要小心處理 Unicode
和所有這些不同的字元編碼