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 条评论
登录 后参与评论

相关文章

来自专栏前端说吧

Sass-学习笔记【基础篇】

4074
来自专栏deepcc

用 Function.apply() 的参数数组化来提高 JavaScript程序性能

3425
来自专栏Laoqi's Linux运维专列

sed命令扩展–转载

文本处理工具之二 sed命令详解 sed:Stream Editor文本流编辑,sed是一个“非交互式的”面向字符流的编辑器。能同时处理多个文件多行的内容,可以...

3085
来自专栏积累沉淀

JavaScript基础1

什么是Javascript? Javascript是一种基于对象和事件驱动的, 与平台无关的 ,具有安全性的 ,弱类型的脚本语言。 为什么要用? 使...

2465
来自专栏Coding迪斯尼

Reactjs开发自制编程语言Monkey的编译器:高能技术干货之语法高亮1

1403
来自专栏PhpZendo

带你玩转 JavaScript ES6 (七) – 异步

本章我们将学习 ES6 中的 Promise(异步) 相关知识,了解如何使用 Promise 对象创建异步程序

691
来自专栏码洞

《快学 Go 语言》第 10 课 —— 错误与异常

Go 语言的异常处理语法绝对是独树一帜,在我见过的诸多高级语言中,Go 语言的错误处理形式就是一朵奇葩。一方面它鼓励你使用 C 语言的形式将错误通过返回值来进行...

933
来自专栏偏前端工程师的驿站

JS魔法堂:判断节点位置关系

一、前言                             在polyfill querySelectorAll 和写弹出窗时都需要判断两个节点间的位置关...

2215
来自专栏我的技术专栏

《effective Go》读后记录:GO基础

2474
来自专栏Jackson0714

PHP内核之旅-4.可变长度的字符串

1283

扫码关注云+社区

领取腾讯云代金券