我想分割一个文本文件。它来作为一个大段落。我想把它分成更小的句子,每个句子都是一个列表。从那里我可以找出哪些列表包含特定的单词。将文本段落拆分成句子
这是我的代码,因为它是目前:
import string
Done = False
while not Done:
try:
File = input("Enter your file: ")
Open_File = open(File, "r")
Info = Open_File.readline()
print(Info)
Open_File.close()
Done = True
except FileNotFoundError:
print("Sorry that file doesn't exist!")
Info_Str = str(Info)
Info_Str = Info_Str.lower()
Info_Str = Info_Str.replace("'", "")
Info_Str = Info_Str.replace("-", "")
Info_Str = Info_Str.split()
Info_List = Info_Str
Info_List = [''.join(c for c in s if c not in string.punctuation) for s in Info_List]
New_List = [item for item in Info_List if not item.isdigit()]
for word in New_List[:]:
if len(word) < 3:
New_List.remove(word)
print(New_List)
如果我把一个文本文件,它只返回一个文本文件的第一行字的列表。
如何将每个单独的句子转换为单独的单词列表?提前致谢。
您确切的要求是什么?如果您只想获取文件中的单词列表,则可以只读取所有行并使用空格分隔符进行分隔。 – Geetanjali
我基本上必须找出哪个行号出现一个特定的单词。每一行都是一个单独的句子。 – Amaranthus
检查我发布的代码段。这应该有所帮助。 – Geetanjali