1500字范文,内容丰富有趣,写作好帮手!
1500字范文 > python编程开发之textwrap文本样式处理技巧

python编程开发之textwrap文本样式处理技巧

时间:2021-09-18 00:34:28

相关推荐

python编程开发之textwrap文本样式处理技巧

后端开发|Python教程

python,编程开发,textwrap,文本样式,Python文本样式

后端开发-Python教程

微信三级分销商城源码,vscode 更改中办发文,ubuntu杀病毒,idea的tomcat不见,爬虫工具查询,php kindle,厦门运营抖音seo优化价位lzw

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

数游源码,如何远程ubuntu桌面,客户端内置tomcat,phython爬虫实战,php是哪个地方,魔贝课凡seo第12期下载lzw

在这里我做了一个demo:

客户信息易语言源码,vscode 卸载扩展,ubuntu gtkmm,tomcat 执行zip,sqlite数据可视化,dz论坛多城市插件,兼容手机pc端的前端框架,我的世界如何刷爬虫,用php 的公司,视频seo排名技巧,wordpress小说网站模板下载地址,个人网页7 留言板asp,移动端商城网站模板lzw

textwrap提供了一些方法:

wrap(text, width = 70, **kwargs):这个函数可以把一个字符串拆分成一个序列

from textwrap import *#使用textwrap中的wrap()方法def test_wrap(): test_str = \\ 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 e 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 \ print(wrap(test_str, 20))def main(): test_wrap()if __name__ == \__main__: main()

输出效果:

Python 3.3.2 (v3.3.2:d047928ae3f6, May 16 , 00:03:43) [MSC v.1600 32 bit (Intel)] on win32Type "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, he work, and two, utility functions,, dedent() and, indent(). If 2, you e just wrapping, or filling one or, wo text strings,, he convenience, functions should be, good 3 enough;, otherwise, you, should use an, instance of, TextWrapper for, efficiency. 4]>>>

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

fill(text, width=70, **kwargs) :该方法可以根据指定的长度,进行拆分字符串,然后逐行显示

from textwrap import *#fill()方法def test_wrap(): test_str = \\ 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 e 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 \ print(fill(test_str, 40))def main(): test_wrap()if __name__ == \__main__: main()

运行效果:

Python 3.3.2 (v3.3.2:d047928ae3f6, May 16 , 00:03:43) [MSC v.1600 32 bit (Intel)] on win32Type "copyright", "credits" or "license()" for more information.>>> ================================ RESTART ================================>>> The textwrap module provides twoconvenience functions, wrap() andfill(), as well as 1 TextWrapper,the class that does all the work, andtwo utility functions, dedent() andindent(). If 2 you e just wrappingor filling one or two text strings, theconvenience functions should be good 3enough; otherwise, you should use aninstance of TextWrapper for efficiency.>>>

dedent()方法->文本进行不缩进显示,相应的indent()方法 -> 进行缩进显示

from textwrap import *#dedent()方法def test_wrap(): test_str = \\ 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 e 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 \ print(repr(dedent(test_str)))def main(): test_wrap()if __name__ == \__main__: main()

运行效果:

Python 3.3.2 (v3.3.2:d047928ae3f6, May 16 , 00:03:43) [MSC v.1600 32 bit (Intel)] on win32Type "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 e 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>>>

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。