python编程开发之textwrap文本样式处理技巧
作者:Hongten 发布时间:2022-03-20 18:48:26
标签:python,文本样式
本文实例讲述了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.
>>>
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'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程序设计有所帮助。
0
投稿
猜你喜欢
- 前言做网络爬虫的同学肯定见过各种各样的验证码,比较高级的有滑动、点选等样式,看起来好像挺复杂的,但实际上它们的核心原理还是还是很清晰的,本文
- 使用ES做搜索引擎拉取数据的时候,如果数据量太大,通过传统的from + size的方式并不能获取所有的数据(默认最大记录数10000),因
- 最近一直在研究 Javascript 相关的技术。在《Javascript 高级程序设计》有篇章节着重阐述了优化 Javascri
- python可以返回多个值,确实挺方便函数里的return只能返回一个值,但是返回类型是没是限制的因此,我们可以“返回一个 tuple类型,
- 今天做了一个很简单的小项目,感受到了paramiko模块的强大,也深感自己Linux的功力不行~~一、需求二、简单需求分析及流程图需求很少,
- 题目:请求出任意区间[a,b]的所有素数,简单考虑实用性这道题看起来应该很easy是吧,但任意区间(这个问题有没get 到)Afanty的分
- 本文实例讲述了Python处理命令行参数模块optpars用法。分享给大家供大家参考,具体如下:optpars是python中用来处理命令行
- 如何用拦截表单的方法上传图片? 看下列代码:<script language="
- pygame绘制机制简介 屏幕控制 pygame.display• 用来控制Pygame游戏的屏幕• Pygame有且只有一个屏幕
- 我们知道,在调用函数时如果不指定某个参数,Python 解释器会抛出异常。为了解决这个问题,Python 允许为参数设置默认值,即在定义函数
- 利用''%.af''%b——其中 b 代表要限定的数字, a 代表要求限定小数点的位数,结果自动四舍五入。例
- ActiveServerPages,ASP0126(0x80004005)-->找不到包含文件MicrosoftOLEDBProvid
- 1. 常用模块# 连接数据库connect()函数创建一个新的数据库连接对话并返回一个新的连接实例对象PG_CONF_123 = {
- 导语元宵节,又称上元节、灯节,是春节之后的第一个重要节日。相传,汉文帝(前179—前157年)为庆祝周勃于正月十五勘平诸
- 无头模式添加,可以让selenium模拟登录,进入到后台运行这里以登录打开公司内网下载数据为例,因为涉及私密问题,所以有些地方我们进行覆盖,
- 我就废话不多说了,直接上代码吧!# -*- coding: utf-8 -*-"""Created on Sa
- 1.1 简介深层神经网络一般都需要大量的训练数据才能获得比较理想的结果。在数据量有限的情况下,可以通过数据增强(Data Augmentat
- 在html里的每一个标签都有其自身的意义,而H标签作为标题标签,它的意义更是至关重要。对于H标签的用法特别是h1的用法一直是个争议的问题,也
- 代码如下:--PK select * from sys.key_constraints where object_id = OBJECT_
- 目前防采集的方法有很多种,先介绍一下常见防采集策略方法和它的弊端及采集对策: 一、判断一个IP在一定时间内对本站页面的访问次数,如果明显超过