我的代码有问题。我有一个文本文件,并且在此文本文件中是一千个来自句子的带标签的/下一行的单词。我的问题是我想还原此文本文件中的单词,然后再次使其成为句子。
我想过一种制作for循环语句的方法,如果它到达点with('test','r') as f:
text = f.open()
sentence = []
sentences = []
for words in text:
if words != "."
sentence.append(words)
elif words == "."
sentence.append(words)
sentences.append(sentence)
sentence = []
#Sample output
#[['This', 'is', 'a', 'sentence', '.'], ['This', 'is', 'the', 'second', 'sentence', '.'],
#['This', 'is', 'the', 'third', 'sentence', '.']],
,那么它将把句子存储在列表中。
#This is the text file
This
is
a
sentence
.
This
is
the
second
sentence
.
This
is
thr
third
sentence
.
{{1}}
代码有点奏效,但是有点复杂。我发现了一个简短而又不太复杂的主意。预先谢谢你。
答案 0 :(得分:0)
您可以使用str.split()
。
例如:
text = 'First sentence. Second sentence. This is the third sentence. '
text.split('. ')[:-1]
>>> ['First sentence', 'Second sentence', 'This is the third sentence']
如果要包含。必须这样做:
text = 'First sentence. Second sentence. This is the third sentence. '
split_text = [e+'.' for e in text.split('. ')][:-1]
split_text
>>> ['First sentence.', 'Second sentence.', 'This is the third sentence.']
答案 1 :(得分:0)
以下是同一个衬里,如果需要更多帮助,请告诉我:
sentences = open('test','r').read().split('\.')
答案 2 :(得分:0)
这非常简单。从文件中读取,按句点分成几行,按任何空格分隔每一行,用单空格重新加入行,将句点退回到句子末尾。
sentences = [' '.join(x.split()) + '.' for x in open('test','r').read().split('.')]