Python 2.7 のドキュメントを読んだ学んだことまとめ
Unicode HOWTO — Python 2.7.13 documentation
https://docs.python.org/2/howto/unicode.html7.8. codecs — Codec registry and base classes — Python 2.7.13 documentation
https://docs.python.org/2/library/codecs.html#encodings-and-unicode
歴史的経緯
1968 年頃
ASCII(American Standard Code for Information Interchange) によって 0-127 の数字が文字に割り当てられていた。
例) a: 97
$ python -V
Python 2.7.10
>>> unichr(97)
u'a'
>>> ord('a')
97
unichr(i) - 2. Built-in Functions — Python 2.7.13 documentation
ord(i) - 2. Built-in Functions — Python 2.7.13 documentation
だがヨーロッパで使われる é やロシアのキリル文字は表せなかった。
1980 年代
8bit (2^8 = 256) のコンピューターが主流になり、128-255 にそれぞれが独自の形式で文字が割り当てていた。
この違いを解消するために Unicode が開発されることになった。
Unicode
定義
The Unicode standard describes how characters are represented by code points.
Character: a
code points: 97 (0x61)
使用する数
当初、Unicode は 16bit (65,536)を使用していた。
現在は 0–1,114,111(0x10ffff) の幅を持っている。
a Unicode string is a sequence of code points, which are numbers from 0 to 0x10ffff.
Encodings
The rules for translating a Unicode string into a sequence of bytes are called an encoding.
>>> 'a'.encode('hex')
'61'
Python コード
$ python -V
Python 2.7.10
>>> s = 'a b c x y z'
>>> s.encode('hex')
'612062206320782079207a'
vim - Insert モード
CTRL-v + u0061
で a
が入力される。