2016-04-16 13 views
1

Ich habe eine Zeichenfolge mit Text und \ n's drin. Wie:Wie konvertiert man einen String in eine Liste, geteilt durch die Zeichenkette ' n n n n n'?

"Information moreinformation moreinformation \n\n\n\n\n Information moreinformation moreinformation \n\n\n\n\n Information moreinformation moreinformation" 

Jetzt möchte ich eine Liste, geteilt durch die \ n \ n \ n \ n \ n Zeichenfolgen. Also ich will:

['Information moreinformation moreinformation', 'Information moreinformation moreinformation', 'Information moreinformation moreinformation'] 

Ich habe das schon versucht, aber das funktioniert nicht. Dann habe ich noch eine Schnur.

with open ("file.txt", "r") as file: 
    content = file.readlines() 
    content = ' '.join(content) 
    content.split('\n\n\n\n\n') 

print (content) 
+3

Strings sind ** unveränderlich **. Alle Operationen an Strings * geben * das Ergebnis zurück, anstatt das Argument zu ändern. Daher: 'content = content.split ('...')' macht den Trick. – Bakuriu

+0

Danke! Es hat funktioniert :) –

+0

Geist ein Beispiel für Ihre Textdatei zu veröffentlichen? ... Vielleicht kann dies besser gelöst werden –

Antwort

0
content = content.split('\n\n\n\n\n') 

bereits. Sie haben den Datentyp der Inhaltsvariablen sonst nicht geändert.

+1

Vielen Dank! Es funktionierte! –