045:文字列を段落に分ける
段落にはおおまかに次の二つの定義あります。
1.日本語ルール 改行(\n)が段落の区切りを示す。
2.英語ルール 空行が段落の区切りを示す。
(1)日本語ルール
splitメソッドをを使います。
セパレータを明示的に書いておく必要があります。セパレータを書かないと任意の空白(スペース、タブ、改行、リターン、改頁)で 区切られてしまします。
http://www.python.jp/doc/release/lib/module-string.html
>>> str
'aa a\nbbb\nccc\n'
>>> str='aa a\nbbb\nccc\n'
>>> str.split()
['aa', 'a', 'bbb', 'ccc']
>>> str.split('\n')
['aa a', 'bbb', 'ccc', '']
>>> print string.split(str,'\n')
['aa a', 'bbb', 'ccc', '']
(2)英語ルール
これも簡単です。
>>> text='This is a pen.\nThat is a pen.\n\nIt is a pen,too.'
>>> text
'This is a pen.\nThat is a pen.\n\nIt is a pen,too.'
>>> print text
This is a pen.
That is a pen.
It is a pen,too.
>>> print text.split('\n\n')
['This is a pen.\nThat is a pen.', 'It is a pen,too.']
>>> print string.split(text,'\n\n')
['This is a pen.\nThat is a pen.', 'It is a pen,too.']