如何从单词文件的几行中拆分每个单词? (Python) [英] How to split each words from several lines of a word file? (python)

查看:69
本文介绍了如何从单词文件的几行中拆分每个单词? (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()方法将该行拆分为单词列表.该程序应建立单词列表.对于每一行中的每个单词,请检查该单词是否已经在列表中,如果没有,则将其附加到列表中.程序完成后,按字母顺序对结果单词进行排序和打印.

Instruction: Open the file and read it line by line. For each line, split the line into a list of words using the split() method. The program should build a list of words. For each word on each line check to see if the word is already in the list and if not append it to the list. When the program completes, sort and print the resulting words in alphabetical order.

所需的输出:

['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) 

推荐答案

我了解您要实现的目标.除了编写方式以外,这里还有一种更简单的方法:

I understand what you are trying to achieve. Instead of the way you wrote it, here is a simpler approach:

import re
ls=set(re.findall(r"[\w']+", text)) #text is the input
print(sorted(ls))

对其进行了测试以确保其正常工作:

Tested it to make sure it works:

我对您的代码做了一些修改,以满足您的用例.

I modified your code a bit to satisfy your use case.

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']

希望能解决您的问题.

这篇关于如何从单词文件的几行中拆分每个单词? (Python)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆