猿问

如何从word文件的几行中拆分每个单词?(Python)

我有一个文本文件:


But soft what light through yonder window breaks

It is the east and Juliet is the sun

Arise fair sun and kill the envious moon

Who is already sick and pale with grief

说明:打开文件,逐行阅读。对于每一行,使用 split() 方法将该行拆分为单词列表。该程序应该建立一个单词列表。对于每一行的每个单词,检查该单词是否已经在列表中,如果不在列表中,则将其添加到列表中。程序完成后,按字母顺序排序并打印生成的单词。


期望的输出:


['Arise', 'But', 'It', 'Juliet', 'Who', 'already', 'and', 'breaks', 'east', 'envious', 'fair', 'grief', 'is', 'kill', 'light', 'moon', 'pale', 'sick', 'soft', 'sun', 'the', 'through', 'what', 'window', 'with', 'yonder']

我被困在这里:


fname = input("Enter file name: ") 

fh = open(fname)

lst = list()

for line in fh:

    line=line.rstrip()

    lst = line.split()

    lst.append(line)

    lst.sort()

print(lst) 


江户川乱折腾
浏览 153回答 3
3回答

守着一只汪

line.split() 为您提供一个列表,该列表将作为列表对象添加到您的 lst 列表中。因此,而不是使用 lst.append(line) 使用 lst.extend(line) 来获得正确的输出。

隔江千里

我了解您要实现的目标。这里有一个更简单的方法,而不是你写的方式:import rels=set(re.findall(r"[\w']+", text)) #text is the inputprint(sorted(ls))测试它以确保它有效:编辑:我稍微修改了您的代码以满足您的用例。fh = open(raw_input("Enter file name: "),'r')lst = list()for line in fh:    words = line[:-1].split(" ")    for word in words:        if word not in lst:            lst.append(word)print(sorted(lst))输出:Enter file name: file.txt['Arise', 'But', 'It', 'Juliet', 'Who', 'already', 'and', 'breaks', 'east', 'envious', 'fair', 'grie', 'is', 'kill', 'light', 'moon', 'pale', 'sick', 'soft', 'sun', 'the', 'through', 'what', 'window', 'with', 'yonder']希望能解决您的问题。

HUH函数

output = []with open('file_name') as f:    for i in f.readlines():        for j in words_to_split:            i = ''.join(i.split(j))        output.append(i)
随时随地看视频慕课网APP

相关分类

Python
我要回答