textwrap 文本包装和填充

高洛峰
发布: 2016-10-20 09:18:09
原创
1452人浏览过

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.

这样第一行就不会缩进。

电子手机配件网站源码1.0
电子手机配件网站源码1.0

电子手机配件网站源码是一个响应式的织梦网站模板,软件兼容主流浏览器,且可以在PC端和手机端中进行浏览。模板包含安装说明,并包含测试数据。本模板基于DEDECms 5.7 UTF-8设计,需要GBK版本的请自己转换。模板安装方法:1、下载最新的织梦dedecms5.7 UTF-8版本。2、解压下载的织梦安装包,得到docs和uploads两个文件夹,请将uploads里面的所有文件和文件夹上传到你的

电子手机配件网站源码1.0 0
查看详情 电子手机配件网站源码1.0

结合移除缩进和填充:

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)

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

最佳 Windows 性能的顶级免费优化软件
最佳 Windows 性能的顶级免费优化软件

每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。

下载
来源:php中文网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
开源免费商场系统广告
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号