python开发_textwrap文本样式
在看python的API的时候,发现python的textwrap在处理字符串样式的时候功能强大
在这里我做了一个demo:
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’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
'''
print(wrap(test_str, 20)) def main():
test_wrap() if __name__ == '__main__':
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) :该方法可以根据指定的长度,进行拆分字符串,然后逐行显示
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’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
'''
print(fill(test_str, 40)) def main():
test_wrap() if __name__ == '__main__':
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
>>>
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’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
'''
print(repr(dedent(test_str))) def main():
test_wrap() if __name__ == '__main__':
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'
>>>
python开发_textwrap文本样式的更多相关文章
- 前端开发CSS3——文本样式和盒子及样式
		
博主废话少说,直接介绍css常用的属性和属性值:属性和值只需过一遍,页面的结构还是需要布局,布局的只是后期会更新的. 提供一些图标的网站:font-awesome: http://fontaw ...
 - python开发_tkinter_获取文本框内容_给文本框添加键盘输入事件
		
在之前的blog中有提到python的tkinter中的菜单操作 python开发_tkinter_窗口控件_自己制作的Python IDEL_博主推荐 python开发_tkinter_窗口控件_自 ...
 - Python 45 长度及颜色单位 、字体样式 、文本样式 、背景样式 、css基础选择器
		
一:长度及颜色单位 长度单位 px(像素) in(英寸) pt(点),一个标准的长度单位,1pt = 1/72in mm(毫米) cm ...
 - Python开发【前端】:jQuery
		
jQuery简介 jQuery是一个快速.简洁的JavaScript框架,是继Prototype之后又一个优秀的JavaScript代码库(或JavaScript框架).jQuery设计的宗旨是&qu ...
 - Python开发【前端】:DOM
		
DOM(文档对象模型(Document Object Model)) 文档对象模型(Document Object Model,简称DOM),是W3C组织推荐的处理可扩展标志语言的标准编程接口.在网页 ...
 - Python开发接水果小游戏
		
我研发的Python游戏引擎Pylash已经更新到1.4了.如今我们就来使用它完毕一个极其简单的小游戏:接水果. 下面是游戏截图: 游戏操作说明:点击屏幕左右两边或者使用键盘方向键控制人物移动.使人物 ...
 - python开发最受欢迎的十款工具
		
python开发最受欢迎的十款工具 dreamyla3个月前 今天小编给正在学习python开发的朋友介绍十款最受欢迎的开发工具,因为在学习python开发过程中少不了IDE或者代码编辑器,想要提高开 ...
 - python 开发工具简介
		
一.python 开发工具简介 1.IDLE IDLE是开发python程序的基本IDE(集成开发环境),具备基本的IDE的功能,是非商业Python开发的不错的选择.当安装好python以后,IDL ...
 - Python 开发工具推荐
		
对于开发工具,仁者见仁智者见智,关键是自己喜欢,用着顺手就好,不用刻意去追求别人用的是什么工具. 这里给大家主要推荐三款工具,分别是PyCharm.Sublime Text 3.VS Code,因为这 ...
 
随机推荐
- Python中单引号,双引号,三引号
			
1.单引号与双引号的区别 s1=‘let‘s go’(明显我们是想用单引号表示let’s go这个字符串的,但是python只知道用‘’来表示字符串,所以python就把字符串中的‘字符当成单引号处理 ...
 - discuz各个目录与文件的作用说明
			
discuz下面有很多文件夹以及文件,你们都知道他们是做什么的么?肯定不知道了吧.但是我们有经常遇到这些文件,譬如在后台文件校验操作都遇到某些文件被修改,这时候也需要知道这些文件是有什么作用的.今天就 ...
 - 关于SQLite3 编译及交叉编译的一些问题
			
from : http://blog.sina.com.cn/s/blog_5f2e119b0101ibwn.html SQLite3 (http://www.sqlite.org)是一个非常强大的小 ...
 - Linux命令学习手册-route命令
			
route [-CFvnee] route [-v] [-A family] add [-net|-host] target [netmask Nm] [gw Gw] [metric N] [mss ...
 - Perl 连接Oracle 出现OCI missing的问题及解决
			
问题描述 新申请了一个虚拟机操作系统: Win Server 2008, 64位 , 8核, 16G Memory 上 http://www.activestate.com/activeperl 下载 ...
 - 用tomcat配置https自签名证书,解决 ios7.1以上系统, 苹果inHouse发布
			
用tomcat配置https自签名证书,解决 ios7.1以上系统苹果inHouse发布不能下载安装的问题教程,话说,我其实最讨厌配置某某环境了,因为某一个小环节一旦出错,你的所有工作往往会功亏一篑, ...
 - android4.0 锁屏实现(转)
			
转载请表明出处:http://blog.csdn.net/wdaming1986/article/details/8837023 好了,言归正传,说说锁屏了,其实把锁屏做成apk的形式,会引起很多问题 ...
 - CentOS7 安装 chrome-gnome-shell
			
1.download https://gitlab.gnome.org/GNOME/chrome-gnome-shell 2.安装cmake和jq yum install -y cmake yum i ...
 - HttpServletRequest继承字ServletRequest的常用方法
			
//获取请求的完整路径String origUrl = req.getRequestURL().toString(); //获取查询的参数String queryStr = req.getQueryS ...
 - phpstorm配置关联php手册
			
最近发现有些编辑器可以 选中函数名,通过相应的快捷键就可以调用 浏览器 打开相应 函数的 在线帮助文档. 一番查找,我终于发现 phpStorm 也有相应的功能. 一.自带功能 在编辑器中选中函数名, ...