≡
  • 网络编程
  • 数据库
  • CMS技巧
  • 软件编程
  • PHP笔记
  • JavaScript
  • MySQL
位置:首页 > 网络编程 > Python

python textwrap文本包装和填充的简单示例

人气:545 时间:2018-09-28

这篇文章主要为大家详细介绍了python textwrap文本包装和填充的简单示例,具有一定的参考价值,可以用来参考一下。

感兴趣的小伙伴,下面一起跟随四海网的小编罗X来看看吧。
对python这个高级语言感兴趣的小伙伴,下面一起跟随四海网的小编两巴掌来看看吧!

代码实例:


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.
'''

段落填充:


# @param python模块学习- textwrap 文本包装和填充
# @author 四海网|www.q1010.com 

import textwrap
from textwrap_exampleimport sample_text
  
print 'Nodedent:\n'
printtextwrap.fill(sample_text, width=50)

# End www_512pic_com

 

执行结果:

# 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.

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

移除缩进:


# @param python模块学习- textwrap 文本包装和填充
# @author 四海网|www.q1010.com 

import textwrap
fromtextwrap_example import sample_text
  
dedented_text = textwrap.dedent(sample_text)
print 'Dedented:'
printdedented_text

# End www_512pic_com

执行结果:

# 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.

这样第一行就不会缩进。

结合移除缩进和填充:


# @param python模块学习- textwrap 文本包装和填充
# @author 四海网|www.q1010.com 

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

# End www_512pic_com

 

执行结果:

# 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.

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


# @param python模块学习- textwrap 文本包装和填充
# @author 四海网|www.q1010.com 

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.

# End www_512pic_com

其中的’’*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)

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

本文来自:http://www.q1010.com/181/1832-0.html

注:关于python textwrap文本包装和填充的简单示例的内容就先介绍到这里,更多相关文章的可以留意四海网的其他信息。

关键词:textwrap

您可能感兴趣的文章

上一篇:python中set的用法:创建的简单示例
下一篇:python os模块功能和方法的简单示例
热门文章
  • Python 处理Cookie的菜鸟教程(一)Cookie库
  • python之pandas取dataframe特定行列的简单示例
  • Python解决json.dumps错误::‘utf8’ codec can‘t decode byte
  • Python通过pythony连接Hive执行Hql的脚本
  • Python 三种方法删除列表中重复元素的简单示例
  • python爬虫代码示例
  • Python 中英文标点转换示例
  • Python 不得不知的开源项目解析
  • Python urlencode编码和url拼接实现方法
  • python按中文拆分中英文混合字符串的简单示例
  • 最新文章
    • Python利用numpy三层神经网络的简单示例
    • pygame可视化幸运大转盘的简单示例
    • Python爬虫之爬取二手房信息的简单示例
    • Python之time库的简单示例
    • OpenCV灰度、高斯模糊、边缘检测的简单示例
    • Python安装Bs4及使用的简单示例
    • django自定义manage.py管理命令的简单示例
    • Python之matplotlib 向任意位置添加一个子图(axes)的简单示例
    • Python图像标签标注软件labelme分析的简单示例
    • python调用摄像头并拍照发邮箱的简单示例

四海网收集整理一些常用的php代码,JS代码,数据库mysql等技术文章。