Skip to content

Commit

Permalink
unicodeの記述を削除
Browse files Browse the repository at this point in the history
該当画像が存在せず、リンクが不正な状態になっていたため、画像を追加
  • Loading branch information
Akira-Taniguchi committed Apr 30, 2016
1 parent 1770209 commit 8db4dc3
Show file tree
Hide file tree
Showing 4 changed files with 0 additions and 74 deletions.
47 changes: 0 additions & 47 deletions source/2_types.rst
Original file line number Diff line number Diff line change
Expand Up @@ -288,54 +288,7 @@ Pythonのスライスを使えば、 :numref:`slice-string` のように、2、3
>>> '-'.join(['pain', 'de', 'campagne'])
'pain-de-campagne'
Unicode文字列型(unicode)
==========================================
Unicode文字列型を使えば、マルチバイトの文字列を扱えます。
先頭に ``u`` を付けたシングルクォート(``'``)、ダブルクォート(``"``)で指定します(:numref:`guide-unicode`)。

.. _guide-unicode:

.. code-block:: python
:caption: Unicode文字列型
>>> u"日本"
u'\u65e5\u672c'
>>> print u"日本"
日本
Unicode文字列型もシーケンス型なので、インデックス表記やスライスが使えます。

エンコードとデコード
------------------------

Unicode文字列型は `Unicode <http://www.unicode.org/>`_ を扱うデータ型です。

Unicode文字列を通常の8ビット文字列にするには、エンコーディング(utf-8やeuc-jpなど)を指定して変換する必要があります。

Unicodeから8ビット文字列に変換する処理を「エンコード」、逆に8ビット文字列からUnicodeに変換する処理を「デコード」と呼びます。

Python でUnicode 文字列をエンコードするには、Unicode文字列型の ``.encode()`` メソッドを呼び出します。

引数にはエンコーディングを指定します。utf-8でエンコーディングするには、 :numref:`encode-unicode` のように指定します。

.. _encode-unicode:

.. code-block:: python
:caption: Unicode文字列のエンコード
>>> u"日本".encode('utf-8')
'\xe6\x97\xa5\xe6\x9c\xac'
文字列からUnicode 文字列型に変換するには、文字列型の ``.decode()`` メソッドを、エンコーディングを指定して呼び出します(:numref:`decode-string`)。

.. _decode-string:

.. code-block:: python
:caption: 文字列のデコード
>>> '\xe6\x97\xa5'.decode('utf-8')
u'\u65e5'
まとめ
===========
Expand Down
27 changes: 0 additions & 27 deletions source/4_files_and_module.rst
Original file line number Diff line number Diff line change
Expand Up @@ -152,33 +152,6 @@ Pythonでファイルを開くには ``open()`` 関数を使います。
Hello world
Hello world
Unicode文字列の場合
------------------------------

ファイルに書き込まれる内容がUnicode文字列の場合は、 ``.encode()``/``.decode()`` を行う必要があります。

utf-8で書き込まれたファイルを読み込んだ場合は、:numref:`utf8-file-read` のように ``.decode()`` を使います。

.. _utf8-file-read:

.. code-block:: python
:caption: utf-8エンコードで書かれたファイルの読み込み
>>> f = open('readme.txt')
>>> readme_text = f.read().decode('utf-8')
>>> print readme_text
リードミー
Unicode文字列を書き込む場合は ``.encode()`` を使います(:numref:`utf8-file-write`)。

.. _utf8-file-write:

.. code-block:: python
:caption: utf-8エンコードでのファイルの書き込み
>>> f = open('readme.txt', 'w')
>>> readme_text = u"リードミー"
>>> f.write(readme_text.encode('utf-8'))
.. _guide-module:

Expand Down
Binary file added source/images/indexing.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added source/images/pythonforwindows1.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 8db4dc3

Please sign in to comment.