简体   繁体   English

如何从python中的文本文件返回单词列表

[英]How to return a list of words from a text file in python

I want to return all words found in a text file. 我想返回在文本文件中找到的所有单词。 This is the code i have so far. 这是我到目前为止的代码。

def get_dictionary_word_list():
    f = open('dictionary.txt')
    for word in f.read().split():
        print(word)

It works using the print fucntion but instead of printing the words i want to return all the words in the text file. 它使用print fucntion但不是打印我想要返回文本文件中所有单词的单词。 Using return it only shows 'aa' and not the words in the file. 使用return它只显示'aa'而不是文件中的单词。 Im not sure why it's not working with return? 我不知道为什么它不与返回一起工作?

If you used return in the loop, it returned on the first iteration and you only got back the first word. 如果你在循环中使用return,它会在第一次迭代时返回,你只返回第一个单词。

What you want is an aggregation of the words - or better yet, return the array you got back from splitting the words. 你想要的是单词的聚合 - 或者更好的是,返回你从分裂单词中获得的数组。 You may want to sanitize line breaks. 您可能想要清理换行符。

def get_dictionary_word_list():
    # with context manager assures us the
    # file will be closed when leaving the scope
    with open('dictionary.txt') as f:
        # return the split results, which is all the words in the file.
        return f.read().split()

To get a dictionary back, you can use this (takes care of line breaks): 要获得字典,您可以使用它(处理换行符):

def get_dictionary_word_list():
    # with context manager assures us the
    # file will be closed when leaving the scope

    with open('dictionary.txt') as f:
        # create a  dictionary object to return
        result = dict()
        for line in f.read().splitlines():
            # split the line to a key - value.
            k, v = line.split()
            # add the key - value to the dictionary object
            result[k]  = v
        return result

To get key,value items back, you can use something like this to return a generator (keep in mind the file will be left open as long as the generator remains open). 为了获得关键的值项,你可以使用这样的东西来返回一个生成器 (请记住,只要生成器保持打开状态,文件就会保持打开状态)。 You can modify it to return just words if that's what you want, it's pretty straightforward: 您可以修改它以返回单词,如果这是您想要的,它非常简单:

def get_dictionary_word_list():
    # with context manager assures us the
    # file will be closed when leaving the scope
    with open('dictionary.txt') as f:
        for line in f.read().splitlines():
            # yield a tuple (key, value)
            yield tuple(line.split())

Example output for the first function: 第一个函数的示例输出:

xxxx:~$ cat dictionary.txt 
a asd
b bsd
c csd
xxxx:~$ cat ld.py 
#!/usr/bin/env python

def get_dictionary_word_list():
    # with context manager assures us the
    # file will be closed when leaving the scope
    with open('dictionary.txt') as f:
        # return the split results, which is all the words in the file.
        return f.read().split()

print get_dictionary_word_list()
xxxx:~$ ./ld.py 
['a', 'asd', 'b', 'bsd', 'c', 'csd']

How about this: 这个怎么样:

def get_dictionary_word_list(fname):
    with open(fname) as fh:
        return set(fh.read().split())
def get_dictionary_word_list():
    f = open('dictionary.txt')
    ll=[]
    for word in f.read().split():
        ll.append(word)
    return ll

Try through a list 尝试列表

Simply try this:- 试试这个: -

def func():
    with open('new.txt') as f:
        return f.read() # returns complete file,

with open('out.txt', 'w+') as w:
    w.write(func())
    w.seek(0)
    print w.read()

with Generators :- Generators : -

def func():
    with open('new.txt') as f:
        yield f.read()
data = func()
with open('out2.txt', 'w+') as w:
    for line in data:
        w.write(line) #or you may use  map(w.write, line)
        w.seek(0)
        print w.read()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM