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程序设计有所帮助。


猜你喜欢
- 资源预加载组件——preload队列,可以支持队列加载和回调,也可以加载视频或者音频进度条,可以动态获取进度条信息支持img标签的预加载,添
- 原则一:注意WHERE子句中的连接顺序: ORACLE采用自下而上的顺序解析WHERE子句,根据这个原理,表之间的连接必须写在其他WHERE
- 前言这里给大家介绍一下利用Pycharm如何打包文件,并为文件设置图标。一、下载pyinstaller库1)点击win+r,输入cmd打开控
- 前提条件:本地已经安装好oracle单实例,能使用plsql developer连接,或者能使用TNS连接串远程连接到oracle集群读取e
- <div style='display:none'> <script type="text/ja
- 前言序锦在编程中,经常要用到字符串的相互转换,现在在这里记录一下Python里面的字符串和整数以及浮点型数之间是如何进行相互转换的。int(
- 早就听说requests的库的强大,只是还没有接触,今天接触了一下,发现以前使用urllib,urllib2等方法真是太搓了……这里写些简单
- 1.什么是MD5加密MD5消息摘要算法(MD5 Message-Digest Algorithm),一种被广泛使用的密码散列函数,可以产生出
- 缩进Python最具特色的是用缩进来标明成块的代码。我下面以if选择结构来举例。if后面跟随条件,如果条件成立,则执行归属于if的一个代码块
- 本文实例讲述了python文件操作之目录遍历的方法。分享给大家供大家参考。具体分析如下:Python的os模块,包含了普遍的操作系统功能,这
- 工厂模式(Factory Pattern)是什么工厂模式是一种创建型模式,它提供了一种创建对象的最佳方式。在工厂模式中,我们在创建对象时不会
- Django 基本命令本节主要是为了让您了解一些django最基本的命令,请尝试着记住它们,并且多多练习下1. 新建一个 django pr
- 2003年以来,网页的平均尺寸已经增长3倍。从2003到2008,网页的平均尺寸从93.7K增至312K,增幅233%。同时,在这5年之内,
- 一个很普通的网页中显示LOGO图像,按照以往的页面制作经验,基本是在页面中插入图像即可(<img src="logo.gif
- 一 概念1. 原理2. 好处不同项目可能用到的环境不同,运用虚拟环境能将不同环境分隔开二 virtualenvvirtual 虚拟的1. 安
- 简介 本次项目登录注册验证是对之前学习知识点的加深学习,这次项目的练习的知识点有函数、判断语句、循环语句、文件操作等。项目流程 运行代码之后
- 1. 原理对于DNA序列,一阶马尔科夫链可以理解为当前碱基的类型仅取决于上一位碱基类型。如图1所示,一条序列的开端(由B开始)可能是A、T、
- 本文实例讲述了python实现生成Word、docx文件的方法。分享给大家供大家参考,具体如下:http://python-docx.rea
- 1.什么是Pillow首先我们需要了解一下PIL(Python Imaging Library),它是Python2中非常强大的图像处理标准
- 注:IE8以前的版本均不支持该特性为了向文档中插入生成内容,可以使用:before与:after伪元素。如,我想在所有链接的后面加上&quo