LoginSignup
0
0

More than 5 years have passed since last update.

Python2で文字コードを自在に操りたい

Last updated at Posted at 2017-05-28

目的

  • Python2で文字列を自在に操りたい
  • 文字コードってなんなん

結論

strをdecodeするとunicode
'\xe3\x81\x82'をdecodeするとu'\u3042'

unicodeをencodeするとstr
u'\u3042'をencodeすると'\xe3\x81\x82'

文字コードとは

文字を画像としてではなく、記号として扱うときに、文字に対応するコードのこと

バイト文字列とユニコード文字列

リテラル Type 表記
バイト文字列 'あいう' str '\xe3\x81\x82'
ユニコード文字列 u'あいう' unicode u'\u3042'

変換方法

>>> # decode : str -> unicode
>>> 'あ'.decode('utf-8')
u'\u3042'
>>> # encode : unicode -> str
>>> u'あ'.encode('utf-8')
'\xe3\x81\x82'

疑問点・調べることリスト

  • 文字コードの調べ方
  • \u3042と\xe3\x81\x82の違いって何?どうやって01で表現するの?
  • 文字コードの歴史(単純に興味ある)

文字コードの本ないかなあと思ったらちょうどよさそうなのがあった。
プログラマのための文字コード技術入門

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0