用多个字符拆分句子,并在每个句子的末尾将它们连接起来



我有一个包含1000个句子的文本文件。我把'中的句子分开,但也有"?">'!'在这些句子中可用。我还需要把它们分开,并在句子末尾加入这些字符。例如:text="我有一个朋友。我有很多朋友!我今天很开心。你开心吗?我希望你开心。">输出:

I have a friend.
I have many friends!
I am very happy today.
Are you happy?
I wish you were.

我试过的代码是

textsentences = text.split('.')
I have tried this code.
with open("file.txt",'w') as writefile:
for line in textsentences:
line = line.strip()
writefile.write("%s . n" % line)

它只适用于一个角色。

这应该可以工作。line_start变量是为了避免句子开头出现不必要的空格:

text = "I have a friend. I have many friends! I am very happy today. Are you happy? I wish you were."
line_start = False
with open("file.txt", 'w') as writefile:
for i in range(len(text)):
if text[i].isalnum() or text[i] == ' ':
if line_start == True and text[i] == ' ':
line_start = False
continue
writefile.write(text[i])
else:
writefile.write(text[i] +'n')
line_start = True

输出:file.txt

I have a friend.
I have many friends!
I am very happy today.
Are you happy?
I wish you were.

相关内容

最新更新