textwrap.wrap(text,[width[,…]]) 算法
import textwrap sample_text = '''aaabbbcccdddeeeedddddfffffggggghhhhhhkkkkkkk''' sample_text2 = '''aaa bbb ccc ddd eeee ddddd fffff ggggg hhhhhh kkkkkkk''' print(sample_text) print(textwrap.wrap(sample_text,width=5)) print(textwrap.wrap(sample_text2,width=5))
testwrap.fill(text,[width])app
import textwrap sample_text = '''aaabbbcccdddeeeedddddfffffggggghhhhhhkkkkkkk''' sample_text2 = '''aaa bbb ccc ddd eeee ddddd fffff ggggg hhhhhh kkkkkkk''' print(sample_text) print(textwrap.fill(sample_text,width=5)) print(textwrap.fill(sample_text2,width=5))
textwrap.dedent(text) ide
import textwrap sample_text = ''' aaabbb cccdddee eedddddfffffggggghhhhhhkkkkkkk''' print(textwrap.dedent(sample_text))
经过构造函数直接初始化函数
TextWrapper(initial_indent="* ")字体
或初始化后赋值spa
textWrap = textwrap.TextWrapper() textWrap.expand_tabs = Truecode
属性以下:blog
width:宽度默认为70字符串
expand_tabs:默认为true,若是设置为true,那么字符串里的全部制表符都会被变成空格,至关于用了字符串方法的expandtabs()。string
replace_whitespace:若是设置为true,那么就会把字符串中的空白符转化为空格。
drop_whitespace:默认true,若是设置为true,则每行开头和结尾的空白符都会被去掉,若是要去掉的空白符占据了整行,那么就会把整行去掉。
initial_indent:在第一行开头插入指定字符串
subsequent_indent:除了第一行在每一行开头插入指定字符串
fix_sentence_endings:默认为false,若是为true,那么就会试图检查每一个句子的结尾是两个空格分割,这个只在等宽字体里被须要。可是这个检查的算法是有缺陷的,它假设了句子是以.!?等这些字符为结尾。
break_long_words:默认为true,切断长句子来让每行的数据宽度都小于width。
break_on_hyphens:连字符相关,默认true