textwrap函数

有时候需要对文本进行长度限制,避免每一行太长(影响阅读), 有什么好的方法吗?

纯Python的写法:

样例代码: 按指定宽度显示文本内容
# -.- coding:utf-8 -.-
__author__ = ‘zt‘
text = ‘Use of this source code is governed by a BSD-style license that can be found in the LICENSE file.‘

width = 35
lines = len(text) // width
if len(text) % width:
    lines = (len(text) // width) + 1

for i in range(lines):
    print text[width * i:width * (i + 1)]

        
输出结果:
Use of this source code is governed
 by a BSD-style license that can be
 found in the LICENSE file.

函数式写法:

样例代码: 按指定宽度显示文本内容
# -.- coding:utf-8 -.-
__author__ = ‘zt‘

def wrap(text, width):
    # 初始化结果变量
    result = ‘‘
    
    # 计算应该迭代次数(即计算出需要输出几行)
    lines = len(text) // width

    # 求余, 如果余数大于0, 就在lines变量上+1
    if len(text) % width:
        lines = (len(text) // width) + 1

    # 按提供的width对原始文本进行切割.
    for i in range(lines):
        result += text[width * i:width * (i + 1)]+"\n"
    return result

if __name__ == ‘__main__‘:
    text = ‘Use of this source code is governed by a BSD-style license that can be found in the LICENSE file.‘    
    print wrap(text, 50)
    
    
# 输出结果
Use of this source code is governed by a BSD-style
 license that can be found in the LICENSE file.

我不知道上述这两种写法是不是唯一的,但是我知道python函数库已经提供了textwrap函数,可以很简单的完成相同工作.

代码样例: 
# -.- coding:utf-8 -.-
__author__ = ‘zt‘
import textwrap

text = ‘Use of this source code is governed by a BSD-style license that can be found in the LICENSE file.‘
print ‘\n‘.join(textwrap.wrap(text, width=50))

输出结果:
Use of this source code is governed by a BSD-style
license that can be found in the LICENSE file.
时间: 2024-08-25 15:20:07

textwrap函数的相关文章

关于matlab GUI 的一些总结

最近在 用matlab做一个GUI的用户界面 大多都是百度出来 摸索的学习 总结一下遇到的一些问题和解决方法 一: 在文本框动态显示文本 (保留之前的文本并显示当前文本内容): 在文本框显示文本 基本的语法是 set(handles.listbox2,'String',string); <1>想要保留之前文本内容  用过网上提供的代码 textwrap函数 (但是 当从 str =get(handles.listbox2,'String'); 得到的str放到string{}里面 textwr

[转载]Matlab之静态文本多行输出

转载文章,原文链接:Matlab中的静态文本框中显示多行内容 有时候,我们在GUI中利用静态文本框显示程序的结果,但是结果很长,一行未必可以显示的开,而静态文本框不像edit或listbox那样通过滚动条来显示多行内容,即便设置了max和min属性也是一样的. 于是,怎么在静态文本框中显示多行是很有意义的. 解决方法 利用函数textwrap 1 2 3 4 5 6 figure('units', 'normalized', 'position', [0.4 0.4 0.4 0.3]); h =

python textwrap.md

textwrap textwrap模块可以用来格式化文本, 使其在某些场合输出更美观. 他提供了一些类似于在很多文本编辑器中都有的段落包装或填充特性的程序功能. Example Data 本节中的示例使用模块textwrap_example.py,它包含一个字符串sample_text. sample_text = ''' The textwrap module can be used to format text for output in situations where pretty-pr

1.3.2 常用内置函数

常用内置函数(Built-In Functions,BIF)不需要导入任何模块即可直接使用,在IDLE中执行如下命令可以列出所有内置函数和内置对象,如代码块1.3.2.1所示: 1 >>> dir(__builtins__) 2 ['ArithmeticError', 'AssertionError', 'AttributeError', 'BaseException', 'BlockingIOError', 'BrokenPipeError', 'BufferError', 'Byte

js高阶函数

map()方法定义在JavaScript的Array中,我们调用Array的map()方法,传入我们自己的函数,就得到了一个新的Array作为结果: function pow(x) { return x * x; } var arr = [1, 2, 3, 4, 5, 6, 7, 8, 9]; arr.map(pow); // [1, 4, 9, 16, 25, 36, 49, 64, 81] reduce()把一个函数作用在这个Array的[x1, x2, x3...]上,这个函数必须接收两个

Django url 标签和reverse()函数的使用(转)

原文:http://www.yihaomen.com/article/python/355.htm 使用url标签和reverse()函数,可以避免在模板和view中对url进行硬编码,这样即使url改变了,对模板和view也没有影响 起初用django 开发应用的时候,完全是在urls.py 中硬编码配置地址,在views.py中HttpResponseRedirect()也是硬编码转向地址,当然在template 中也是一样了,这样带来一个问题,如果在urls.py 中修改了某个页面的地址,

Python2.7-内置函数

具体参见:https://docs.python.org/2/library/functions.html#file 1.进制转换:bin(x), oct(x), hex(x) 把一个十进制数分别转换为2.8.16进制 2.字符转换:chr(x)将数字(255以内不报错,128以后无字符)转换为对应ASCII字符, unichr(x)将数字转换为unicode, ord(x) 将字符转数字与前两个相反, unicode(obj, [encoding, [error]]) 用encoding解码o

linux Shell函数

Shell函数类似于Shell脚本,里面存放了一系列的指令,不过Shell的函数存在于内存,而不是硬盘文件,所以速度很快,另外,Shell还能对函数进行预处理,所以函数的启动比脚本更快. 1.函数定义 1 2 3 4 function 函数名() {     语句     [return] } 关键字function表示定义一个函数,可以省略,其后是函数名,有时函数名后可以跟一个括号,符号"{"表示函数执行命令的入口,该符号也可以在函数名那一行,"}"表示函数体的结

pythonの函数学习笔记(一)

函数是可以实现一些特定功能的小方法或小程序定义函数function的方法:def function_name(arg1,arg2[,...]): statement [return value]注意事项:1.def开头,代表定义函数,def和函数名中间要敲一个空格:2.返回值不是必须的,如果没有renturn语句,则默认返回值None:3.函数名必须以下划线或字母开头,可以包含任意字母.数字或下划线的组合,区分大小写且不能是保留字: py使用名称空间的概念存储对象,这个名称空间就是对象作用的区域