专栏首页Hongtenpython开发_textwrap文本样式

python开发_textwrap文本样式

在看python的API的时候,发现python的textwrap在处理字符串样式的时候功能强大

在这里我做了一个demo:

textwrap提供了一些方法:

wrap(text, width = 70, **kwargs):这个函数可以把一个字符串拆分成一个序列
 1 from textwrap import *
 2 
 3 #使用textwrap中的wrap()方法
 4 def test_wrap():
 5     test_str = '''\
 6     The textwrap module provides two convenience functions, wrap() and fill(), as well as 1
 7     TextWrapper, the class that does all the work, and two utility functions, dedent() and indent(). If 2
 8     you’re just wrapping or filling one or two text strings, the convenience functions should be good 3
 9     enough; otherwise, you should use an instance of TextWrapper for efficiency. 4
10     '''
11     print(wrap(test_str, 20))
12 
13 def main():
14     test_wrap()
15 
16 if __name__ == '__main__':
17     main()

输出效果:

Python 3.3.2 (v3.3.2:d047928ae3f6, May 16 2013, 00:03:43) [MSC v.1600 32 bit (Intel)] on win32
Type "copyright", "credits" or "license()" for more information.
>>> ================================ RESTART ================================
>>> 
['    The textwrap', 'module provides two', 'convenience', 'functions, wrap()', 'and fill(), as well', 'as 1', 'TextWrapper, the', 'class that does all', 'the work, and two', 'utility functions,', 'dedent() and', 'indent(). If 2', 'you’re just wrapping', 'or filling one or', 'two text strings,', 'the convenience', 'functions should be', 'good 3     enough;', 'otherwise, you', 'should use an', 'instance of', 'TextWrapper for', 'efficiency. 4']
>>> 

我们会发现,wrap()函数,把字符串拆分成了一个序列,在这个序列中,每个元素的长度是一样的。

fill(text, width=70, **kwargs) :该方法可以根据指定的长度,进行拆分字符串,然后逐行显示
 1 from textwrap import *
 2 
 3 #fill()方法
 4 def test_wrap():
 5     test_str = '''\
 6     The textwrap module provides two convenience functions, wrap() and fill(), as well as 1
 7     TextWrapper, the class that does all the work, and two utility functions, dedent() and indent(). If 2
 8     you’re just wrapping or filling one or two text strings, the convenience functions should be good 3
 9     enough; otherwise, you should use an instance of TextWrapper for efficiency. 4
10     '''
11     print(fill(test_str, 40))
12    
13 def main():
14     test_wrap()
15 
16 if __name__ == '__main__':
17     main()

运行效果:

Python 3.3.2 (v3.3.2:d047928ae3f6, May 16 2013, 00:03:43) [MSC v.1600 32 bit (Intel)] on win32
Type "copyright", "credits" or "license()" for more information.
>>> ================================ RESTART ================================
>>> 
    The textwrap module provides two
convenience functions, wrap() and
fill(), as well as 1     TextWrapper,
the class that does all the work, and
two utility functions, dedent() and
indent(). If 2     you’re just wrapping
or filling one or two text strings, the
convenience functions should be good 3
enough; otherwise, you should use an
instance of TextWrapper for efficiency.
4
>>> 
dedent()方法->文本进行不缩进显示,相应的indent()方法 -> 进行缩进显示
 1 from textwrap import *
 2 
 3 #dedent()方法
 4 def test_wrap():
 5     test_str = '''\
 6     The textwrap module provides two convenience
 7         functions, wrap() and fill(), as well as 1
 8     TextWrapper, the class that does all the work,
 9         and two utility functions, dedent() and indent(). If 2
10     you’re just wrapping or filling one or two text strings,
11         the convenience functions should be good 3
12     enough; otherwise, you should use an instance
13         of TextWrapper for efficiency. 4
14     '''
15     print(repr(dedent(test_str)))
16 
17 def main():
18     test_wrap()
19 
20 if __name__ == '__main__':
21     main()

运行效果:

Python 3.3.2 (v3.3.2:d047928ae3f6, May 16 2013, 00:03:43) [MSC v.1600 32 bit (Intel)] on win32
Type "copyright", "credits" or "license()" for more information.
>>> ================================ RESTART ================================
>>> 
'The textwrap module provides two convenience\n    functions, wrap() and fill(), as well as 1\nTextWrapper, the class that does all the work,\n    and two utility functions, dedent() and indent(). If 2\nyou’re just wrapping or filling one or two text strings,\n    the convenience functions should be good 3\nenough; otherwise, you should use an instance\n    of TextWrapper for efficiency. 4\n'
>>> 

本文参与腾讯云自媒体分享计划,欢迎正在阅读的你也加入,一起分享。

我来说两句

0 条评论
登录 后参与评论

相关文章

  • python开发_sqlite3_绝对完整_博主推荐

    =========================================

    Hongten
  • python开发_python代码风格(coding style)

    Hongten
  • python开发_搜索本地文件信息写入文件

    =======================================================

    Hongten
  • 求完全二叉树其节点的个数

    大学里的混子
  • MySql and or优先级

    最近在使用MySql的时候,发现MySql在处理and和or关键词的时候,and优先级高于or。即,在一条SQL语句中,出现了多个and和or的时候,会优先执行...

    幽鸿
  • 对登录中账号密码进行加密之后再传输的爆破的思路和方式

    一. 概述 渗透测试过程中遇到web登录的时候,现在很多场景账号密码都是经过js加密之后再请求发送(通过抓包可以看到加密信息)如图一burp抓到的包,reque...

    FB客服
  • kill.exe溢出漏洞分析与EXP讨论

    * 本文原创作者:zzz66686,本文属FreeBuf原创奖励计划,未经许可禁止转载 1. 前言 前几日,笔者在exploit-db上发现了一个kill.ex...

    FB客服
  • 使用Go语言编译互联网视频直播点播平台EasyDSS出现ld returned 1 exit status错误

    由于Go语言非常易学,且代码精简,我们很多同事在研发视频平台的时候,都喜欢拿Go语言来做编程。我们之前也讲过关于GO语言编译的相关问题,比如:Go语言编程iou...

    EasyNVR
  • 黑客入门,从HTB开始

    Hack the box 是国外的一个靶机平台,里面的靶机包含多种系统类型,并且里面可以利用的漏洞类型多种多样,有很多靶机其实非常贴近实战情景。因此 HTB 是...

    madneal
  • 【Python 第51课】 and-or技巧

    今天介绍一个python中的小技巧:and-or 看下面这段代码: a = "heaven" b = "hell" c = True and a or b p...

    Crossin先生

扫码关注云+社区

领取腾讯云代金券