mirror of
https://github.com/fofolee/uTools-Manuals.git
synced 2025-06-08 06:55:36 +08:00
29 lines
30 KiB
HTML
29 lines
30 KiB
HTML
<div class="body" role="main"><div class="section" id="module-textwrap"><h1><span class="yiyi-st" id="yiyi-10">6.4. <a class="reference internal" href="#module-textwrap" title="textwrap: Text wrapping and filling"><code class="xref py py-mod docutils literal"><span class="pre">textwrap</span></code></a> - 文本换行和填充</span></h1><p><span class="yiyi-st" id="yiyi-11"><strong>源代码:</strong> <a class="reference external" href="https://hg.python.org/cpython/file/3.5/Lib/textwrap.py">Lib/textwrap.py</a></span></p><p><span class="yiyi-st" id="yiyi-12"><a class="reference internal" href="#module-textwrap" title="textwrap: Text wrapping and filling"><code class="xref py py-mod docutils literal"><span class="pre">textwrap</span></code></a>模块提供一些方便的函数,以及<a class="reference internal" href="#textwrap.TextWrapper" title="textwrap.TextWrapper"><code class="xref py py-class docutils literal"><span class="pre">TextWrapper</span></code></a>类,所有实际工作都是由这个类完成的。</span><span class="yiyi-st" id="yiyi-13">如果你只是包装或填充一个或两个文本字符串,那么这些便利的函数就足够了;否则,你应该使用 <a class="reference internal" href="#textwrap.TextWrapper" title="textwrap.TextWrapper"><code class="xref py py-class docutils literal"><span class="pre">TextWrapper</span></code></a> 的一个实例来完成任务以确保效率。</span></p><dl class="function"><dt id="textwrap.wrap"><span class="yiyi-st" id="yiyi-14"><code class="descclassname">textwrap.</code><code class="descname">wrap</code><span class="sig-paren">(</span><em>text</em>, <em>width=70</em>, <em>**kwargs</em><span class="sig-paren">)</span></span></dt><dd><p><span class="yiyi-st" id="yiyi-15">将单个段落包含在<em>text</em>(一个字符串)中,以使每行最多<em>width</em>个字符长。</span><span class="yiyi-st" id="yiyi-16">返回输出行的列表,不包含最终换行符。</span></p><p><span class="yiyi-st" id="yiyi-17">可选的关键字参数对应于<a class="reference internal" href="#textwrap.TextWrapper" title="textwrap.TextWrapper"><code class="xref py py-class docutils literal"><span class="pre">TextWrapper</span></code></a>的实例属性,如下所述。</span><span class="yiyi-st" id="yiyi-18"><em>width</em>默认为<code class="docutils literal"><span class="pre">70</span></code>。</span></p><p><span class="yiyi-st" id="yiyi-19">有关<a class="reference internal" href="#textwrap.wrap" title="textwrap.wrap"><code class="xref py py-func docutils literal"><span class="pre">wrap()</span></code></a>行为的更多详细信息,请参阅<a class="reference internal" href="#textwrap.TextWrapper.wrap" title="textwrap.TextWrapper.wrap"><code class="xref py py-meth docutils literal"><span class="pre">TextWrapper.wrap()</span></code></a>方法。</span></p></dd></dl><dl class="function"><dt id="textwrap.fill"><span class="yiyi-st" id="yiyi-20"><code class="descclassname">textwrap.</code><code class="descname">fill</code><span class="sig-paren">(</span><em>text</em>, <em>width=70</em>, <em>**kwargs</em><span class="sig-paren">)</span></span></dt><dd><p><span class="yiyi-st" id="yiyi-21">将单个段落包含在<em>文本</em>中,并返回包含包装段落的单个字符串。</span><span class="yiyi-st" id="yiyi-22"><a class="reference internal" href="#textwrap.fill" title="textwrap.fill"><code class="xref py py-func docutils literal"><span class="pre">fill()</span></code></a>是下面语句的简写形式</span></p><pre><code class="language-python"><span></span><span class="s2">"</span><span class="se">\n</span><span class="s2">"</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="n">wrap</span><span class="p">(</span><span class="n">text</span><span class="p">,</span> <span class="o">...</span><span class="p">))</span>
|
||
</code></pre><p><span class="yiyi-st" id="yiyi-23">特别地,<a class="reference internal" href="#textwrap.fill" title="textwrap.fill"><code class="xref py py-func docutils literal"><span class="pre">fill()</span></code></a>接受与<a class="reference internal" href="#textwrap.wrap" title="textwrap.wrap"><code class="xref py py-func docutils literal"><span class="pre">wrap()</span></code></a>完全相同的关键字参数。</span></p></dd></dl><dl class="function"><dt id="textwrap.shorten"><span class="yiyi-st" id="yiyi-24"><code class="descclassname">textwrap.</code><code class="descname">shorten</code><span class="sig-paren">(</span><em>text</em>, <em>width</em>, <em>**kwargs</em><span class="sig-paren">)</span></span></dt><dd><p><span class="yiyi-st" id="yiyi-25">折叠并截断给定的<em>文本</em>以适合给定的<em>宽度</em>。</span></p><p><span class="yiyi-st" id="yiyi-26">首先,<em>文本</em>中的空格将折叠(所有空格都由单个空格替换)。</span><span class="yiyi-st" id="yiyi-27">如果结果符合<em>width</em>,则返回。</span><span class="yiyi-st" id="yiyi-28">否则,从尾部删除足够的单词,以使剩余单词加上<code class="xref py py-attr docutils literal"><span class="pre">placeholder</span></code>适合<code class="xref py py-attr docutils literal"><span class="pre">width</span></code>:</span></p><pre><code class="language-python"><span></span><span class="gp">>>> </span><span class="n">textwrap</span><span class="o">.</span><span class="n">shorten</span><span class="p">(</span><span class="s2">"Hello world!"</span><span class="p">,</span> <span class="n">width</span><span class="o">=</span><span class="mi">12</span><span class="p">)</span>
|
||
<span class="go">'Hello world!'</span>
|
||
<span class="gp">>>> </span><span class="n">textwrap</span><span class="o">.</span><span class="n">shorten</span><span class="p">(</span><span class="s2">"Hello world!"</span><span class="p">,</span> <span class="n">width</span><span class="o">=</span><span class="mi">11</span><span class="p">)</span>
|
||
<span class="go">'Hello [...]'</span>
|
||
<span class="gp">>>> </span><span class="n">textwrap</span><span class="o">.</span><span class="n">shorten</span><span class="p">(</span><span class="s2">"Hello world"</span><span class="p">,</span> <span class="n">width</span><span class="o">=</span><span class="mi">10</span><span class="p">,</span> <span class="n">placeholder</span><span class="o">=</span><span class="s2">"..."</span><span class="p">)</span>
|
||
<span class="go">'Hello...'</span>
|
||
</code></pre><p><span class="yiyi-st" id="yiyi-29">可选的关键字参数对应于<a class="reference internal" href="#textwrap.TextWrapper" title="textwrap.TextWrapper"><code class="xref py py-class docutils literal"><span class="pre">TextWrapper</span></code></a>的实例属性,如下所述。</span><span class="yiyi-st" id="yiyi-30">Note that the whitespace is collapsed before the text is passed to the <a class="reference internal" href="#textwrap.TextWrapper" title="textwrap.TextWrapper"><code class="xref py py-class docutils literal"><span class="pre">TextWrapper</span></code></a> <a class="reference internal" href="#textwrap.fill" title="textwrap.fill"><code class="xref py py-meth docutils literal"><span class="pre">fill()</span></code></a> function, so changing the value of <a class="reference internal" href="#textwrap.TextWrapper.tabsize" title="textwrap.TextWrapper.tabsize"><code class="xref py py-attr docutils literal"><span class="pre">tabsize</span></code></a>, <a class="reference internal" href="#textwrap.TextWrapper.expand_tabs" title="textwrap.TextWrapper.expand_tabs"><code class="xref py py-attr docutils literal"><span class="pre">expand_tabs</span></code></a>, <a class="reference internal" href="#textwrap.TextWrapper.drop_whitespace" title="textwrap.TextWrapper.drop_whitespace"><code class="xref py py-attr docutils literal"><span class="pre">drop_whitespace</span></code></a>, and <a class="reference internal" href="#textwrap.TextWrapper.replace_whitespace" title="textwrap.TextWrapper.replace_whitespace"><code class="xref py py-attr docutils literal"><span class="pre">replace_whitespace</span></code></a> will have no effect.</span></p><div class="versionadded"><p><span class="yiyi-st" id="yiyi-31"><span class="versionmodified">新版本3.4.</span></span></p></div></dd></dl><dl class="function"><dt id="textwrap.dedent"><span class="yiyi-st" id="yiyi-32"><code class="descclassname">textwrap.</code><code class="descname">dedent</code><span class="sig-paren">(</span><em>text</em><span class="sig-paren">)</span></span></dt><dd><p><span class="yiyi-st" id="yiyi-33">从<em>文本</em>中的每一行中删除任何常见的前导空格。</span></p><p><span class="yiyi-st" id="yiyi-34">这可以用于使三引号字符串与显示器的左边缘对齐,同时仍以缩进形式在源代码中呈现它们。</span></p><p><span class="yiyi-st" id="yiyi-35">注意,制表符和空格都被视为空格,但它们不相等:<code class="docutils literal"><span class="pre">“</span> <span class="pre">hello”</span></code>和<code class="docutils literal"><span class="pre">"\thello"</span></code>被视为没有共同的前导空格。</span></p><p><span class="yiyi-st" id="yiyi-36">例如:</span></p><pre><code class="language-python"><span></span><span class="k">def</span> <span class="nf">test</span><span class="p">():</span>
|
||
<span class="c1"># end first line with \ to avoid the empty line!</span>
|
||
<span class="n">s</span> <span class="o">=</span> <span class="s1">'''</span><span class="se">\</span>
|
||
<span class="s1"> hello</span>
|
||
<span class="s1"> world</span>
|
||
<span class="s1"> '''</span>
|
||
<span class="nb">print</span><span class="p">(</span><span class="nb">repr</span><span class="p">(</span><span class="n">s</span><span class="p">))</span> <span class="c1"># prints ' hello\n world\n '</span>
|
||
<span class="nb">print</span><span class="p">(</span><span class="nb">repr</span><span class="p">(</span><span class="n">dedent</span><span class="p">(</span><span class="n">s</span><span class="p">)))</span> <span class="c1"># prints 'hello\n world\n'</span>
|
||
</code></pre></dd></dl><dl class="function"><dt id="textwrap.indent"><span class="yiyi-st" id="yiyi-37"><code class="descclassname">textwrap.</code><code class="descname">indent</code><span class="sig-paren">(</span><em>text</em>, <em>prefix</em>, <em>predicate=None</em><span class="sig-paren">)</span></span></dt><dd><p><span class="yiyi-st" id="yiyi-38">将<em>前缀</em>添加到<em>文本</em>中选定行的开头。</span></p><p><span class="yiyi-st" id="yiyi-39">行通过调用<code class="docutils literal"><span class="pre">text.splitlines(True)</span></code>分隔。</span></p><p><span class="yiyi-st" id="yiyi-40">默认情况下,<em>前缀</em>添加到不包含空格(包括任何行结尾)的所有行。</span></p><p><span class="yiyi-st" id="yiyi-41">例如:</span></p><pre><code class="language-python"><span></span><span class="gp">>>> </span><span class="n">s</span> <span class="o">=</span> <span class="s1">'hello</span><span class="se">\n\n</span><span class="s1"> </span><span class="se">\n</span><span class="s1">world'</span>
|
||
<span class="gp">>>> </span><span class="n">indent</span><span class="p">(</span><span class="n">s</span><span class="p">,</span> <span class="s1">' '</span><span class="p">)</span>
|
||
<span class="go">' hello\n\n \n world'</span>
|
||
</code></pre><p><span class="yiyi-st" id="yiyi-42">可选的<em>谓词</em>参数可用于控制哪些行缩进。</span><span class="yiyi-st" id="yiyi-43">例如,很容易将<em>前缀</em>添加到即使为空和纯空格的行:</span></p><pre><code class="language-python"><span></span><span class="gp">>>> </span><span class="nb">print</span><span class="p">(</span><span class="n">indent</span><span class="p">(</span><span class="n">s</span><span class="p">,</span> <span class="s1">'+ '</span><span class="p">,</span> <span class="k">lambda</span> <span class="n">line</span><span class="p">:</span> <span class="kc">True</span><span class="p">))</span>
|
||
<span class="go">+ hello</span>
|
||
<span class="go">+</span>
|
||
<span class="go">+</span>
|
||
<span class="go">+ world</span>
|
||
</code></pre><div class="versionadded"><p><span class="yiyi-st" id="yiyi-44"><span class="versionmodified">版本3.3中的新功能。</span></span></p></div></dd></dl><p><span class="yiyi-st" id="yiyi-45"><a class="reference internal" href="#textwrap.wrap" title="textwrap.wrap"><code class="xref py py-func docutils literal"><span class="pre">wrap()</span></code></a>,<a class="reference internal" href="#textwrap.fill" title="textwrap.fill"><code class="xref py py-func docutils literal"><span class="pre">fill()</span></code></a>和<a class="reference internal" href="#textwrap.shorten" title="textwrap.shorten"><code class="xref py py-func docutils literal"><span class="pre">shorten()</span></code></a>通过创建<a class="reference internal" href="#textwrap.TextWrapper" title="textwrap.TextWrapper"><code class="xref py py-class docutils literal"><span class="pre">TextWrapper</span></code></a>类来实现功能。</span><span class="yiyi-st" id="yiyi-46">创建的TextWrapper实例不会有效的重复使用,因此,对于频繁使用<a class="reference internal" href="#textwrap.wrap" title="textwrap.wrap"><code class="xref py py-func docutils literal"><span class="pre">wrap()</span></code></a>或<a class="reference internal" href="#textwrap.fill" title="textwrap.fill"><code class="xref py py-func docutils literal"><span class="pre">fill()</span></code></a>处理文本字符串的场合,创建一个自己的<a class="reference internal" href="#textwrap.TextWrapper" title="textwrap.TextWrapper"><code class="xref py py-class docutils literal"><span class="pre">TextWrapper</span></code></a>对象来使用效率会更高。</span></p><p><span class="yiyi-st" id="yiyi-47">文本最好包裹在空格上,紧跟在连字符后面;只有在必要时才会断开长字,除非<a class="reference internal" href="#textwrap.TextWrapper.break_long_words" title="textwrap.TextWrapper.break_long_words"><code class="xref py py-attr docutils literal"><span class="pre">TextWrapper.break_long_words</span></code></a>设置为false。</span></p><dl class="class"><dt id="textwrap.TextWrapper"><span class="yiyi-st" id="yiyi-48"><em class="property">class </em><code class="descclassname">textwrap.</code><code class="descname">TextWrapper</code><span class="sig-paren">(</span><em>**kwargs</em><span class="sig-paren">)</span></span></dt><dd><p><span class="yiyi-st" id="yiyi-49"><a class="reference internal" href="#textwrap.TextWrapper" title="textwrap.TextWrapper"><code class="xref py py-class docutils literal"><span class="pre">TextWrapper</span></code></a>构造函数接受多个可选的关键字参数。</span><span class="yiyi-st" id="yiyi-50">每个关键字参数对应一个实例属性,例如</span></p><pre><code class="language-python"><span></span><span class="n">wrapper</span> <span class="o">=</span> <span class="n">TextWrapper</span><span class="p">(</span><span class="n">initial_indent</span><span class="o">=</span><span class="s2">"* "</span><span class="p">)</span>
|
||
</code></pre><p><span class="yiyi-st" id="yiyi-51">是相同的</span></p><pre><code class="language-python"><span></span><span class="n">wrapper</span> <span class="o">=</span> <span class="n">TextWrapper</span><span class="p">()</span>
|
||
<span class="n">wrapper</span><span class="o">.</span><span class="n">initial_indent</span> <span class="o">=</span> <span class="s2">"* "</span>
|
||
</code></pre><p><span class="yiyi-st" id="yiyi-52">您可以多次重复使用相同的<a class="reference internal" href="#textwrap.TextWrapper" title="textwrap.TextWrapper"><code class="xref py py-class docutils literal"><span class="pre">TextWrapper</span></code></a>对象,您可以通过在使用之间直接分配实例属性来更改其任何选项。</span></p><p><span class="yiyi-st" id="yiyi-53"><a class="reference internal" href="#textwrap.TextWrapper" title="textwrap.TextWrapper"><code class="xref py py-class docutils literal"><span class="pre">TextWrapper</span></code></a>实例属性(以及构造函数的关键字参数)如下:</span></p><dl class="attribute"><dt id="textwrap.TextWrapper.width"><span class="yiyi-st" id="yiyi-54"><code class="descname">width</code> </span></dt><dd><p><span class="yiyi-st" id="yiyi-55">(默认值:<code class="docutils literal"><span class="pre">70</span></code>)包装线的最大长度。</span><span class="yiyi-st" id="yiyi-56">只要输入文本中没有长度小于<a class="reference internal" href="#textwrap.TextWrapper.width" title="textwrap.TextWrapper.width"><code class="xref py py-attr docutils literal"><span class="pre">width</span></code></a>的单个单词,<a class="reference internal" href="#textwrap.TextWrapper" title="textwrap.TextWrapper"><code class="xref py py-class docutils literal"><span class="pre">TextWrapper</span></code></a>可保证没有输出行长于<a class="reference internal" href="#textwrap.TextWrapper.width" title="textwrap.TextWrapper.width"><code class="xref py py-attr docutils literal"><span class="pre">width</span></code></a>个字符。</span></p></dd></dl><dl class="attribute"><dt id="textwrap.TextWrapper.expand_tabs"><span class="yiyi-st" id="yiyi-57"><code class="descname">expand_tabs</code> </span></dt><dd><p><span class="yiyi-st" id="yiyi-58">(default: <code class="docutils literal"><span class="pre">True</span></code>) If true, then all tab characters in <em>text</em> will be expanded to spaces using the <code class="xref py py-meth docutils literal"><span class="pre">expandtabs()</span></code> method of <em>text</em>.</span></p></dd></dl><dl class="attribute"><dt id="textwrap.TextWrapper.tabsize"><span class="yiyi-st" id="yiyi-59"><code class="descname">tabsize</code> </span></dt><dd><p><span class="yiyi-st" id="yiyi-60">(默认:<code class="docutils literal"><span class="pre">8</span></code>)如果<a class="reference internal" href="#textwrap.TextWrapper.expand_tabs" title="textwrap.TextWrapper.expand_tabs"><code class="xref py py-attr docutils literal"><span class="pre">expand_tabs</span></code></a>为真,则<em>文本</em>中的所有制表符将扩展为零个或多个空格,列和给定的制表符大小。</span></p><div class="versionadded"><p><span class="yiyi-st" id="yiyi-61"><span class="versionmodified">版本3.3中的新功能。</span></span></p></div></dd></dl><dl class="attribute"><dt id="textwrap.TextWrapper.replace_whitespace"><span class="yiyi-st" id="yiyi-62"><code class="descname">replace_whitespace</code> </span></dt><dd><p><span class="yiyi-st" id="yiyi-63">(默认值:<code class="docutils literal"><span class="pre">True</span></code>)如果为true,在tab展开之后但在换行之前,<a class="reference internal" href="#textwrap.wrap" title="textwrap.wrap"><code class="xref py py-meth docutils literal"><span class="pre">wrap()</span></code></a>方法将用一个空白符替换每个空格。</span><span class="yiyi-st" id="yiyi-64">替换的空格字符如下:制表符,换行符,垂直制表符,换行符和回车符(<code class="docutils literal"><span class="pre">'\t\n\v\f\r'</span></code>)。</span></p><div class="admonition note"><p class="first admonition-title"><span class="yiyi-st" id="yiyi-65">注意</span></p><p class="last"><span class="yiyi-st" id="yiyi-66">如果<a class="reference internal" href="#textwrap.TextWrapper.expand_tabs" title="textwrap.TextWrapper.expand_tabs"><code class="xref py py-attr docutils literal"><span class="pre">expand_tabs</span></code></a>为false,且<a class="reference internal" href="#textwrap.TextWrapper.replace_whitespace" title="textwrap.TextWrapper.replace_whitespace"><code class="xref py py-attr docutils literal"><span class="pre">replace_whitespace</span></code></a>为真,则每个制表符字符将被替换为单个空格,即<em>而不是制表符展开。</em></span></p></div><div class="admonition note"><p class="first admonition-title"><span class="yiyi-st" id="yiyi-67">注意</span></p><p class="last"><span class="yiyi-st" id="yiyi-68">如果<a class="reference internal" href="#textwrap.TextWrapper.replace_whitespace" title="textwrap.TextWrapper.replace_whitespace"><code class="xref py py-attr docutils literal"><span class="pre">replace_whitespace</span></code></a>为false,则换行符可能会出现在行的中间并导致奇怪的输出。</span><span class="yiyi-st" id="yiyi-69">出于这个原因,文本应该被分割成段(使用<a class="reference internal" href="stdtypes.html#str.splitlines" title="str.splitlines"><code class="xref py py-meth docutils literal"><span class="pre">str.splitlines()</span></code></a>或类似)。</span></p></div></dd></dl><dl class="attribute"><dt id="textwrap.TextWrapper.drop_whitespace"><span class="yiyi-st" id="yiyi-70"><code class="descname">drop_whitespace</code> </span></dt><dd><p><span class="yiyi-st" id="yiyi-71">(默认值:<code class="docutils literal"><span class="pre">True</span></code>)如果为true,则删除每行的开头和结尾处的空格(在换行之后但在缩进之前)。</span><span class="yiyi-st" id="yiyi-72">但是,如果非空白跟在它后面,则在段落开头的空格不会被删除。</span><span class="yiyi-st" id="yiyi-73">如果删除的空白占据整个行,则整行将被删除。</span></p></dd></dl><dl class="attribute"><dt id="textwrap.TextWrapper.initial_indent"><span class="yiyi-st" id="yiyi-74"><code class="descname">initial_indent</code> </span></dt><dd><p><span class="yiyi-st" id="yiyi-75">(默认值:<code class="docutils literal"><span class="pre">''</span></code>)将被添加到包装输出的第一行的字符串。</span><span class="yiyi-st" id="yiyi-76">计数到第一行的长度。</span><span class="yiyi-st" id="yiyi-77">空字符串不缩进。</span></p></dd></dl><dl class="attribute"><dt id="textwrap.TextWrapper.subsequent_indent"><span class="yiyi-st" id="yiyi-78"><code class="descname">subsequent_indent</code> </span></dt><dd><p><span class="yiyi-st" id="yiyi-79">(默认:<code class="docutils literal"><span class="pre">''</span></code>)将添加到除第一个之外的所有换行输出行的字符串。</span><span class="yiyi-st" id="yiyi-80">计算每行的长度,除了第一行。</span></p></dd></dl><dl class="attribute"><dt id="textwrap.TextWrapper.fix_sentence_endings"><span class="yiyi-st" id="yiyi-81"><code class="descname">fix_sentence_endings</code> </span></dt><dd><p><span class="yiyi-st" id="yiyi-82">(默认:<code class="docutils literal"><span class="pre">False</span></code>)如果为true,则<a class="reference internal" href="#textwrap.TextWrapper" title="textwrap.TextWrapper"><code class="xref py py-class docutils literal"><span class="pre">TextWrapper</span></code></a>会尝试检测句末,并确保句子总是以两个空格分隔。</span><span class="yiyi-st" id="yiyi-83">这通常对于等宽字体中的文本是期望的。</span><span class="yiyi-st" id="yiyi-84">然而,句子检测算法是不完美的:假设句子结尾由小写字母,后面跟着<code class="docutils literal"><span class="pre">'.'</span></code>,<code class="docutils literal"><span class="pre">'!'</span></code>或<code class="docutils literal"><span class="pre">'?'</span></code>,可能后跟<code class="docutils literal"><span class="pre">'"'</span></code>或<code class="docutils literal"><span class="pre">"'"</span></code>之一,后跟一个空格。</span><span class="yiyi-st" id="yiyi-85">这方面的一个问题是算法是无法检测到“博士”之间的差异</span></p><pre><code class="language-python"><span></span><span class="p">[</span><span class="o">...</span><span class="p">]</span> <span class="n">Dr</span><span class="o">.</span> <span class="n">Frankenstein</span><span class="s1">'s monster [...]</span>
|
||
</code></pre><p><span class="yiyi-st" id="yiyi-86">和“Spot”</span></p><pre><code class="language-python"><span></span><span class="p">[</span><span class="o">...</span><span class="p">]</span> <span class="n">See</span> <span class="n">Spot</span><span class="o">.</span> <span class="n">See</span> <span class="n">Spot</span> <span class="n">run</span> <span class="p">[</span><span class="o">...</span><span class="p">]</span>
|
||
</code></pre><p><span class="yiyi-st" id="yiyi-87">默认情况下,<a class="reference internal" href="#textwrap.TextWrapper.fix_sentence_endings" title="textwrap.TextWrapper.fix_sentence_endings"><code class="xref py py-attr docutils literal"><span class="pre">fix_sentence_endings</span></code></a>为false。</span></p><p><span class="yiyi-st" id="yiyi-88">因为语句检测算法依赖于<code class="docutils literal"><span class="pre">string.lowercase</span></code>来定义“小写字母”,以及在句点之后使用两个空格来分隔同一行上的句子的惯例,所以它特定于英语 - 语言文本。</span></p></dd></dl><dl class="attribute"><dt id="textwrap.TextWrapper.break_long_words"><span class="yiyi-st" id="yiyi-89"><code class="descname">break_long_words</code> </span></dt><dd><p><span class="yiyi-st" id="yiyi-90">(默认值:<code class="docutils literal"><span class="pre">True</span></code>)如果为true,则长度大于<a class="reference internal" href="#textwrap.TextWrapper.width" title="textwrap.TextWrapper.width"><code class="xref py py-attr docutils literal"><span class="pre">width</span></code></a>的字符将被破坏,以确保没有行长度大于<a class="reference internal" href="#textwrap.TextWrapper.width" title="textwrap.TextWrapper.width"><code class="xref py py-attr docutils literal"><span class="pre">width</span></code></a>。</span><span class="yiyi-st" id="yiyi-91">如果为假,长字不会被破坏,并且一些行可能长于<a class="reference internal" href="#textwrap.TextWrapper.width" title="textwrap.TextWrapper.width"><code class="xref py py-attr docutils literal"><span class="pre">width</span></code></a>。</span><span class="yiyi-st" id="yiyi-92">(长字自行放在一条线上,以尽量减少超过<a class="reference internal" href="#textwrap.TextWrapper.width" title="textwrap.TextWrapper.width"><code class="xref py py-attr docutils literal"><span class="pre">width</span></code></a>的量。)</span></p></dd></dl><dl class="attribute"><dt id="textwrap.TextWrapper.break_on_hyphens"><span class="yiyi-st" id="yiyi-93"><code class="descname">break_on_hyphens</code> </span></dt><dd><p><span class="yiyi-st" id="yiyi-94">(默认值:<code class="docutils literal"><span class="pre">True</span></code>)如果为true,则包装将优先在空格中出现,紧跟在复合词中的连字符后面,这是英语中习惯的。</span><span class="yiyi-st" id="yiyi-95">如果为false,则只有空格被视为换行符的潜在好位置,但如果您想要真正不可靠的字词,则需要将<a class="reference internal" href="#textwrap.TextWrapper.break_long_words" title="textwrap.TextWrapper.break_long_words"><code class="xref py py-attr docutils literal"><span class="pre">break_long_words</span></code></a>设置为false。</span><span class="yiyi-st" id="yiyi-96">以前版本中的默认行为是始终允许断开连字符词。</span></p></dd></dl><dl class="attribute"><dt id="textwrap.TextWrapper.max_lines"><span class="yiyi-st" id="yiyi-97"><code class="descname">max_lines</code> </span></dt><dd><p><span class="yiyi-st" id="yiyi-98">(默认:<code class="docutils literal"><span class="pre">None</span></code>)如果不是<code class="docutils literal"><span class="pre">None</span></code>,则输出将包含最多<em>max_lines</em>行,并显示<em>占位符</em>在输出结束时。</span></p><div class="versionadded"><p><span class="yiyi-st" id="yiyi-99"><span class="versionmodified">新版本3.4.</span></span></p></div></dd></dl><dl class="attribute"><dt id="textwrap.TextWrapper.placeholder"><span class="yiyi-st" id="yiyi-100"><code class="descname">placeholder</code></span></dt><dd><p><span class="yiyi-st" id="yiyi-101">(默认值:<code class="docutils literal"><span class="pre">'</span> <span class="pre">[...]'</span></code>)将在输出文本末尾出现的字符串</span></p><div class="versionadded"><p><span class="yiyi-st" id="yiyi-102"><span class="versionmodified">新版本3.4.</span></span></p></div></dd></dl><p><span class="yiyi-st" id="yiyi-103"><a class="reference internal" href="#textwrap.TextWrapper" title="textwrap.TextWrapper"><code class="xref py py-class docutils literal"><span class="pre">TextWrapper</span></code></a>还提供了一些公共方法,类似于模块级的方便功能:</span></p><dl class="method"><dt id="textwrap.TextWrapper.wrap"><span class="yiyi-st" id="yiyi-104"><code class="descname">wrap</code><span class="sig-paren">(</span><em>text</em><span class="sig-paren">)</span></span></dt><dd><p><span class="yiyi-st" id="yiyi-105">将单个段落包含在<em>文本</em>(字符串)中,以使每行最多<a class="reference internal" href="#textwrap.TextWrapper.width" title="textwrap.TextWrapper.width"><code class="xref py py-attr docutils literal"><span class="pre">width</span></code></a>个字符长。</span><span class="yiyi-st" id="yiyi-106">所有包装选项都取自<a class="reference internal" href="#textwrap.TextWrapper" title="textwrap.TextWrapper"><code class="xref py py-class docutils literal"><span class="pre">TextWrapper</span></code></a>实例的实例属性。</span><span class="yiyi-st" id="yiyi-107">返回输出行的列表,不包含最终换行符。</span><span class="yiyi-st" id="yiyi-108">如果包装的输出没有内容,则返回的列表为空。</span></p></dd></dl><dl class="method"><dt id="textwrap.TextWrapper.fill"><span class="yiyi-st" id="yiyi-109"><code class="descname">fill</code><span class="sig-paren">(</span><em>text</em><span class="sig-paren">)</span></span></dt><dd><p><span class="yiyi-st" id="yiyi-110">将单个段落包含在<em>文本</em>中,并返回包含包装段落的单个字符串。</span></p></dd></dl></dd></dl></div></div> |