2016-04-16 21 views
1

文字列に\ nという文字列があります。 Like:文字列をリストに変換するには、文字列 ' n n n n n'で分割しますか?

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

ここでは、リストを\ n \ n \ n \ n \ n文字列で分割します。だから私は欲しい:

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

私はすでにこれを試みたが、これは動作しません。それから私はまだ文字列を持っています。

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

print (content) 
+3

文字列は**不変です**。文字列のすべての操作は、引数を変更する代わりに結果を返します。したがって、 'content = content.split( '...')'がトリックを行います。 – Bakuriu

+0

ありがとう!それは働いた:) –

+0

あなたのテキストファイルのサンプルを投稿する心?...これは良い方法で解決することがあります –

答えて

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

これを試してみてください。それ以外の場合は、コンテンツ変数のデータ型は変更しませんでした。

+1

ありがとう!出来た! –

関連する問題