Python Moduleのtextwrap-テキスト段落フォーマット編成
モジュールの目的:各行のテキストのブレーク位置を調整することで、段落テキストをフォーマットします.
テキスト美観出力(印刷)が必要なシーンでは、
サンプルデータ
このセクションの例では、複数行の文字列
段落の塗りつぶし(Fill)
結果は満足できなかった.テキストは左揃えになりますが、最初の行のテキストは行の先頭のインデントを保持し、残りの行の先頭の空白は段落の真ん中に埋め込まれます.
「空白」(Dedent)を削除
前の例では、フォーマットされたテキスト出力には、いくつかのタブと余分な空白が混ざっているので、美しく見えません.
結果は美しくなり始めました
たとえば、空白の代わりに下線
出力結果は次のとおりです.
DedentとFillを組み合わせて使用
次に、行ヘッダーの空白を除去したテキストを
結果は次のとおりです.
インデントテキストの追加
サンプル段落は、新しい各行に分割され、各行の前に接頭辞
どのローに接頭辞を追加するかを制御する場合は、
その結果、偶数の長さの各行にのみ接頭辞を追加しました.
凸列(段落内縮)
また、
これにより、最初の行のテキストのインデントが他の行よりも少ない「凸列」文字が容易に生成されます.
接頭辞テキストは、アスタリスク
長い文字列の切り捨て
元の文字列の空白以外の文字が除去されると、プレースホルダに置き換えられます.デフォルトのプレースホルダは
原文はこちら
参考:1.textwrapモジュールの公式ドキュメント
テキスト美観出力(印刷)が必要なシーンでは、
textwrap
モジュールを使用して、テキストをフォーマットすることができる.多くのテキストエディタやワープロで使用されている段落のパッケージ、塗りつぶしなどのプログラム化機能に似ています.サンプルデータ
このセクションの例では、複数行の文字列
textwrap_example.py
を含むsample_text
モジュールを構築します.# textwrap_example.py
sample_text = '''
The textwrap module can be used to format text for output in
situations where pretty-printing is desired. It offers
programmatic functionality similar to the paragraph wrapping
or filling features found in many text editors.
'''
段落の塗りつぶし(Fill)
fill()
メソッドは、テキストを入力として使用し、フォーマットされたテキストを返します.# textwrap_fill.py
import textwrap
from textwrap_example import sample_text
print(textwrap.fill(sample_text, width=50))
結果は満足できなかった.テキストは左揃えになりますが、最初の行のテキストは行の先頭のインデントを保持し、残りの行の先頭の空白は段落の真ん中に埋め込まれます.
$ python3 textwrap_fill.py
The textwrap module can be used to format
text for output in situations where pretty-
printing is desired. It offers programmatic
functionality similar to the paragraph wrapping
or filling features found in many text editors.
「空白」(Dedent)を削除
前の例では、フォーマットされたテキスト出力には、いくつかのタブと余分な空白が混ざっているので、美しく見えません.
dedent()
メソッドは、サンプル文字列の各行のテキスト行の先頭の共有空白を除去することができ、結果をより美しく見せることができる.サンプル文字列は、その特性を説明するために人為的に加えられた空白である.# textwrap_dedent.py
import textwrap
from textwrap_example import sample_text
dedented_text = textwrap.dedent(sample_text)
print('Dedented:')
print(dedented_text)
結果は美しくなり始めました
$ python3 textwrap_dedent.py
Dedented:
The textwrap module can be used to format text for output in
situations where pretty-printing is desired. It offers
programmatic functionality similar to the paragraph wrapping
or filling features found in many text editors.
dedent()
(インデント除去)は「indent」(インデント/空白)の対立面であり、dedent()
の方法の結果、各行のテキスト行頭の共有空白が除去される.しかし、1行のテキスト自体が他の行よりも空白が多い場合、これらの空白は除去されません.たとえば、空白の代わりに下線
_
を入力します._Line one.
__Line two.
_Line Three.
出力結果は次のとおりです.
Line one.
_Line two.
Line Three.
DedentとFillを組み合わせて使用
次に、行ヘッダーの空白を除去したテキストを
fill()
メソッドに渡し、異なるwidth
パラメータ値を使用してテストします.# textwrap_fill_width.py
import textwrap
from textwrap_example import sample_text
dedented_text = textwrap.dedent(sample_text).strip()
for width in [45, 60]:
print('{} Columns:
'.format(width))
print(textwrap.fill(dedented_text, width=width))
print()
結果は次のとおりです.
$ python3 textwrap_fill_width.py
45 Columns:
The textwrap module can be used to format
text for output in situations where pretty-
printing is desired. It offers programmatic
functionality similar to the paragraph
wrapping or filling features found in many
text editors.
60 Columns:
The textwrap module can be used to format text for output in
situations where pretty-printing is desired. It offers
programmatic functionality similar to the paragraph wrapping
or filling features found in many text editors.
インデントテキストの追加
indent()
メソッドを使用して、1つの複数行の文字列の各行の先頭に一致する接頭辞テキストを追加できます.次の例では、サンプル文字列の各行に>
プレフィックスを追加し、メール返信で参照されるフォーマットにします.# textwrap_indent.py
import textwrap
from textwrap_example import sample_text
dedented_text = textwrap.dedent(sample_text)
wrapped = textwrap.fill(dedented_text, width=50)
wrapped += '
Second paragraph after a blank line.'
final = textwrap.indent(wrapped, '> ')
print('Quoted block:
')
print(final)
サンプル段落は、新しい各行に分割され、各行の前に接頭辞
>
が付加され、その後、これらの行は新しい文字列を構成して返される.$ python3 textwrap_indent.py
Quoted block:
> The textwrap module can be used to format text
> for output in situations where pretty-printing is
> desired. It offers programmatic functionality
> similar to the paragraph wrapping or filling
> features found in many text editors.
> Second paragraph after a blank line.
どのローに接頭辞を追加するかを制御する場合は、
indent()
メソッドにpredicate
の断言パラメータを渡すことができます.このパラメータは、各ローのテキスト、indent()
メソッドが先にメソッドを呼び出して判断し、メソッドがTrue
を返すと、そのローの前に接頭辞を追加します.そうしないと追加しません.# textwrap_indent_predicate.py
import textwrap
from textwrap_example import sample_text
def should_indent(line):
print('Indent {!r}?'.format(line))
return len(line.strip()) % 2 == 0
dedented_text = textwrap.dedent(sample_text)
wrapped = textwrap.fill(dedented_text, width=50)
final = textwrap.indent(wrapped, 'EVEN ', predicate=should_indent)
print('
Quoted block:
')
print(final)
その結果、偶数の長さの各行にのみ接頭辞を追加しました.
$ python3 textwrap_indent_predicate.py
Indent ' The textwrap module can be used to format text
'?
Indent 'for output in situations where pretty-printing is
'?
Indent 'desired. It offers programmatic functionality
'?
Indent 'similar to the paragraph wrapping or filling
'?
Indent 'features found in many text editors.'?
Quoted block:
EVEN The textwrap module can be used to format text
for output in situations where pretty-printing is
EVEN desired. It offers programmatic functionality
EVEN similar to the paragraph wrapping or filling
EVEN features found in many text editors.
凸列(段落内縮)
また、
fill()
メソッドを使用して接頭辞の追加を実現することもできます.同様に、出力の幅を設定することができ、最初の行のテキストの接頭辞テキストを個別に設定することができます.# textwrap_hanging_indent.py
import textwrap
from textwrap_example import sample_text
dedented_text = textwrap.dedent(sample_text)
print(textwrap.fill(dedented_text, initial_indent='', subsequent_indent=' ' * 4, width=50))
これにより、最初の行のテキストのインデントが他の行よりも少ない「凸列」文字が容易に生成されます.
$ python3 textwrap_hanging_indent.py
The textwrap module can be used to format text for
output in situations where pretty-printing is
desired. It offers programmatic functionality
similar to the paragraph wrapping or filling
features found in many text editors.
接頭辞テキストは、アスタリスク
*
などの非空白文字であってもよく、カラムの要点を生成することができます.長い文字列の切り捨て
shorten()
法を用いて、長い文字列を遮断し、要約または概要を生成することができる.タブ、改行、列のスペースなど、すべての空白文字が1つのスペースに置き換えられます.テキストは要求されたテキストの長さ以下で切断され、切断された場所は単語の境界にあり、不完全な単語の出現を避ける.# textwrap_shorten.py
import textwrap
from textwrap_example import sample_text
dedented_text = textwrap.dedent(sample_text)
original = textwrap.fill(dedented_text, width=50)
print('Original:
')
print(original)
shortened = textwrap.shorten(original, 100)
shortened_wrapped = textwrap.fill(shortened, width=50)
print('
Shortened:
')
print(shortened_wrapped)
元の文字列の空白以外の文字が除去されると、プレースホルダに置き換えられます.デフォルトのプレースホルダは
[...]
であり、shorten()
メソッドにplaceholder
パラメータを渡すことで設定できます.$ python3 textwrap_shorten.py
Original:
The textwrap module can be used to format text
for output in situations where pretty-printing is
desired. It offers programmatic functionality
similar to the paragraph wrapping or filling
features found in many text editors.
Shortened:
The textwrap module can be used to format text for
output in situations where pretty-printing [...]
原文はこちら
参考:1.textwrapモジュールの公式ドキュメント