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

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.

'''

段落填充:

import textwrap  from textwrap_exampleimport sample_text      print 'Nodedent:n'  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.

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

移除缩进:

import textwrap  fromtextwrap_example import 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.

这样第一行就不会缩进。

结合移除缩进和填充:

import textwrap  fromtextwrap_example import sample_text      dedented_text =textwrap.dedent(sample_text).strip()  for width in [ 45,70 ]:         print '%d Columns:n' % 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.

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

import textwrap  fromtextwrap_example import 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 to format text for      output in situations where pretty-printingis      desired. It offers programmatic functionality      similar to the paragraph wrapping orfilling      features found in many text editors.

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

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

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

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

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

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

textwrap.dedent(text)

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

  • 初学者学习python2还是python3?
  • python获取本机IP、mac地址、计算机名
  • 详解python2 和 python3的区别
  • python基础之删除文件及删除目录的方法
  • 用python求第1000个质数的值
  • python常用函数年初大总结
  • Python3 - 时间处理与定时任务
  • Python开发的CMS系统,Silva CMS 3 发布
  • python基础之使用os.system来执行系统命令
  • 判断python字典中key是否存在的两种方法
  • 初学者学习python2还是python3?
  • python基础之删除文件及删除目录的方法
  • python获取本机IP、mac地址、计算机名
  • python获取系统时间(时间函数详解)
  • 详解python2 和 python3的区别
  • 用python求第1000个质数的值
  • Python3 - 时间处理与定时任务
  • 命令行看糗百
  • Python算法之---冒泡,选择,插入排序算法
  • python 中求和函数 sum详解
  • range方法在Python2和Python3中的不同
  • python3 数组(列表)初始化
  • 记一次crontab中date命令错用导致的问题
  • MySQL用LIKE特殊字符搜索
  • CentOS 7 下修改主机名
  • Python3正则表达式之:(?(id/name)y...
  • TIOBE编程语言排行榜2019年 Python稳居前三
  • 解压命令unzip常用方法汇总
  • 解析redis备份文件rdb的两种方法及对比
  • 百度视觉语义化平台2.0:交互升级和...
  • 5G时代的视觉语义化技术:软硬结合...
  • 百度AutoDL重磅升级至3.0:设计、迁...