Python
 Computer >> コンピューター >  >> プログラミング >> Python

Textwrap-Pythonでのテキストの折り返しと入力


textwrapモジュールは、折り返しまたは塗りつぶしを実行するTextWrapperクラスを提供します。同じ目的で便利な機能があります。

wrap(text)

1つの段落をテキスト(文字列)でラップして、すべての行が最大で幅の文字の長さになるようにします。最終的な改行なしで、出力行のリストを返します。

fill(text)

単一の段落をテキストでラップし、ラップされた段落を含む単一の文字列を返します。

>>> sample_text = '''
The textwrap module provides some convenience functions, as well as TextWrapper class
that does all the work. If you’re just wrapping or filling one or two text strings,
the convenience functions should be good enough; otherwise, you should use an instance
of TextWrapper for efficiency.
'''
>>> import textwrap
>>> for line in (textwrap.wrap(sample_text, width = 50)):
print (line)

The textwrap module provides some convenience
functions, as well as TextWrapper class that
does all the work. If you’re just wrapping or
filling one or two text strings, the
convenience functions should be good enough;
otherwise, you should use an instance of
TextWrapper for efficiency.

例を記入

>>> textwrap.fill(sample_text, width = 50)
' The textwrap module provides some convenience\nfunctions, as well as TextWrapper class that\ndoes all the work. If you’re just wrapping or\nfilling one or two text strings, the\nconvenience functions should be good enough;\notherwise, you should use an instance of\nTextWrapper for efficiency.'

  1. 要素とテキストを見つけるためのSeleniumとPython?

    Selenium Webdriverを使用して、要素とそのテキストを見つけることができます。まず、id、classname、cssなどのロケーターを使用して要素を特定する必要があります。次に、テキストを取得するには、テキストを使用する必要があります。 メソッド。 構文 s = driver.find_element_by_css_selector("h4").text ここでドライバー webdriverオブジェクトです。メソッドfind_element_by_css_selector cssロケータータイプで要素を識別するために使用され、ロケーター値は引数としてメソッド

  2. Pythonでの相関と回帰

    相関とは、2つのデータセット間の依存関係を含むいくつかの統計的関係を指します。線形回帰は、従属変数と1つ以上の独立変数の間の関係を確立するための線形アプローチです。単一の独立変数は線形回帰と呼ばれ、複数の独立変数は重回帰と呼ばれます。 相関 依存する現象の簡単な例としては、親と子孫の外見の相関関係、製品の価格と供給量の相関関係などがあります。seabornpythonライブラリで利用可能なアイリスデータセットの例を取り上げます。その中で、3種のアヤメの花のがく片と花びらの長さと幅の相関関係を確立しようとしています。見つかった相関関係に基づいて、ある種を別の種から簡単に区別する強力なモデルを作