python模块学习- textwrap 文本包装和填充

本文介绍了Python的textwrap模块,用于文本包装和填充,实现漂亮的输出格式。通过示例展示了如何使用dedent去除文本的首行缩进,以及fill函数进行文本填充。此外,还演示了如何调整填充宽度和实现悬挂缩进,适用于处理和格式化多行文本内容。
摘要由CSDN通过智能技术生成

python模块学习- textwrap 文本包装和填充

代码实例:

sample_text = '''

   The textwrap module can beused to format text for output in

   situations wherepretty-printing is desired.  It offers

   programmatic functionalitysimilar to the paragraph wrapping

   or filling features found inmany text editors.

'''

段落填充:

1

2

3

4

5

import textwrap

from textwrap_exampleimport sample_text

   

print 'Nodedent: '

printtextwrap.fill(sample_text, width=50)

 

执行结果:

# pythontextwrap_fill.py

No dedent:

 

    The textwrap module can be used to format

text for outputin     situations where pretty-

printing is desired.  It offers    programmatic

functionalitysimilar to the paragraph wrapping

or fillingfeatures found in many text editors.

结果为左对齐,第一行有缩进。行中的空格继续保留。

移除缩进:

1

2

3

4

5

6

import textwrap

fromtextwrap_exampleimport sample_text

   

dedented_text= textwrap.dedent(sample_text)

print 'Dedented:'

printdedented_text

 

执行结果:

# pythontextwrap_dedent.py

Dedented:

 

The textwrapmodule can be used to format text for output in

situations wherepretty-printing is desired.  It offers

programmaticfunctionality similar to the paragraph wrapping

or fillingfeatures found in many text editors.

这样第一行就不会缩进。

结合移除缩进和填充:

1

2

3

4

5

6

7

8

import textwrap

fromtextwrap_exampleimport sample_text

   

dedented_text=textwrap.dedent(sample_text).strip()

for widthin [45,70 ]:

       print '%d Columns: ' % width

       print textwrap.fill(dedented_text,width=width)

       print

 

执行结果:

# pythontextwrap_fill_width.py

45 Columns:

 

The textwrapmodule can be used to format

text for output insituations where pretty-

printing isdesired.  It offers programmatic

functionalitysimilar to the paragraph

wrapping orfilling features found in many

text editors.

 

70 Columns:

 

The textwrapmodule can be used to format text for output in

situations wherepretty-printing is desired.  It offersprogrammatic

functionality similarto the paragraph wrapping or filling features

found in many texteditors.

悬挂缩进:悬挂缩进第一行的缩进小于其他行的缩进。

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

import textwrap

fromtextwrap_exampleimport sample_text

   

dedented_text=textwrap.dedent(sample_text).strip()

printtextwrap.fill(dedented_text,

                    initial_indent='',

                    subsequent_indent=' ' * 4,

                    width=50,

                    )

       执行结果:

# pythontextwrap_hanging_indent.py

The textwrapmodule can be used toformat textfor

    outputin situations where pretty-printingis

    desired. It offers programmatic functionality

    similar to the paragraph wrapping orfilling

    features foundin many text editors.

 

其中的’’*4还可以使用其他字符代替。

 

             TextWrap提供函数wrap()和fill(), 以及TextWrapper类,工具函数dedent(). 通常包装或者填充一两个字符串使用wrap()和fill()。其他情况使用TextWrapper更高效。

 

textwrap.wrap(text[,width[, ...]])

包装单个段落(text为输入,系字符串),每行最长宽度width。返回输出行的列表,最后行无换行符。Width默认70。

 

textwrap.fill(text[,width[, ...]])

包装单段文字,并返回包含包裹段落的字符串。实际上是" ".join(wrap(text,...))的缩写。wrap() and fill()创建TextWrapper实例,并调用一个方法。这些实例不被重用,所以包装/填充很多文本字符串要构造自己的TextWrapper对象更有效。TextWrapper.break_long_words设置是否拆长单词。

textwrap.dedent(text)

反缩进去除每行行首的空白。这方便显示三引号中的内容而不修改其源代码中的缩进。

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

「已注销」

LHStudio感谢您的支持

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值