2017-10-08 9 views
0

文章を区切り記号として区切り文章に分割しようとしています。私はこれまでのコードは動作しますが、デリミタは単独で行に表示されています。どのように文章と一緒に句読点を保持することができますか?テキストファイルを文章に分割する

import re 
string = "" 
with open("text.txt") as file: 
    for line in file: 
     for l in re.split(r"(\. |\? |\!)",line): 
      string += l + "\n" 
print(string) 

出力例:

This is the flag of the Prooshi — ous, the Cap and Soracer 
. 
This is the bullet that byng the flag of the Prooshious 
. 
This is the ffrinch that fire on the Bull that bang the flag of the Prooshious 
. 

答えて

0

それだ実際には文字列に追加されます、あなたがたとえば、あなたがKek.を分割し、各反復の\ nを(改行文字)を追加し、そう単純で変数Kek\n、その後、 .\n。 このような操作を行う必要があります:

with open("text.txt") as file: 
for line in file: 
    for l in re.split(r"(\. |\? |\!)",line): 
     string += l 
    string += '\n' 
関連する問題