2016-09-28 5 views
0

Ich habe einen großen Textblock. Es hat Zeilenumbrüche, aber da die Zeilen trotz der Zeilenumbrüche immer noch zu lang sind, wird es in die nächste Zeile umgebrochen. Da bei allen anderen Skriptfunktionen alle Zeilen um ein Leerzeichen eingerückt sind, möchte ich, dass dies zutrifft. Ich verstehe, dass wenn ich nur eine Zeile drucke, kann ich einfach ein Leerzeichen einfügen, und wenn ich nach einem Zeilenumbruch, die in eine Zeile passen, einrücken möchte, kann ich einfach \n mit einem Leerzeichen nach einfügen.Einrücken auf neue Zeilen in der Konsole

Wie würde ich jede Zeile in einem Textblock einfügen? z:

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"""

das wäre drucken, wie:

>>> 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 

Antwort

0

Ist das, was Sie suchen?

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 

Ergebnisse:

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                                  
+0

Ich schließe nicht importieren oder obere – user6896502

+0

@NateHailfire, welche Version von Python sind Sie? – blackpen

0

Sie sagten, beizutreten, zu importieren scheitern Split. Versuchen Sie Folgendes:

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 
Verwandte Themen