缩进在控制台中的新行

时间:2016-09-28 22:58:23

标签: python console auto-indent

我有一大块文字。它有换行符,但由于换行仍然很长,即使换行也是如此,它会换行到下一行。由于所有其他脚本函数都有所有行缩进一个空格,我希望这与它匹配。据我所知,如果我只打印一行,我可以插入一个空格,如果我想在一行中的换行后缩进,我可以在\n之后插入一个空格。

如何将文本块中的每一行缩进? e.g:

text = """This is a block of text. It keeps going on and on and on. It has some line breaks \n but mostly just keeps going on without breaks. The lines are sometimes too long, so they wrap to the next line, but they don't indent. I need to fix this"""

将打印为:

>>> print(text) 
  This is a block of text. It keeps going on
  and on and on. It has some line breaks

  but mostly just keeps going on without
  breaks. The lines are sometimes too long,
  so they wrap to the next line, but they 
  don't indent. I need to fix this

2 个答案:

答案 0 :(得分:0)

这是你在找什么?

import textwrap
from string import join, split

text = """This is a block of text. It keeps going on
            and on and on. It has    some line breaks \n
            but mostly just keeps going on without
            breaks. The lines are sometimes too long,
            so they wrap to the next line, but they 
            don't indent. I need to fix this"""

print "\nPrinted as one line:\n"
t=join(text.split())
print t

print "\nPrinted as formatted paragraph:\n"
t=join(text.split())
t=textwrap.wrap(t,width=70,initial_indent=' '*4,subsequent_indent=' '*8)
t=join(t,"\n")
print t

结果:

Printed as one line:                                                                                                                                           

This is a block of text. It keeps going on and on and on. It has some line breaks but mostly just keeps going on without breaks. The lines are sometimes too lo
ng, so they wrap to the next line, but they don't indent. I need to fix this                                                                                   

Printed as formatted paragraph:                                                                                                                                

    This is a block of text. It keeps                                                                                                                          
        going on and on and on. It has                                                                                                                         
        some line breaks but mostly just                                                                                                                       
        keeps going on without breaks.                                                                                                                         
        The lines are sometimes too                                                                                                                            
        long, so they wrap to the next                                                                                                                         
        line, but they don't indent. I                                                                                                                         
        need to fix this                                                                                                                                       

答案 1 :(得分:0)

你说加入,拆分无法导入。请尝试以下方法:

import re, textwrap

def myformatting(t):
    t=re.sub('\s+',' ',t); t=re.sub('^\s+','',t); t=re.sub('\s+$','',t)
    t=textwrap.wrap(t,width=40,initial_indent=' '*4,subsequent_indent=' '*8)
    s=""
    for i in (t): s=s+i+"\n"
    s=re.sub('\s+$','',s)
    return(s)

text = """\t\tThis is a block of text. It keeps going on
            and on and on. It has    some line breaks \n
            but mostly just keeps going on without
            breaks. The lines are sometimes too long,
            so they wrap to the next line, but they 
            don't indent. I need to fix this"""

text=myformatting(text)
print text