简体   繁体   English

将所有先前生成的单词存储到单独文件中的随机单词生成器?

[英]Random Word Generator that stores all previously generated words into a separate file?

My Python Level: Beginner我的 Python 级别:初级

MY QUESTION: If I wanted to make a random word generator that could show me all of the generated words previously produced (in a separate CSV file, for example), how would I do that?我的问题:如果我想制作一个随机单词生成器,它可以向我显示之前生成的所有生成的单词(例如,在单独的 CSV 文件中),我该怎么做?

This seems like it should be fairly simple, but I don't know what to look up to solve this problem.这看起来应该相当简单,但我不知道要查找什么来解决这个问题。 My thoughts were to use a loop somehow, keep reassigning values, or just copy/paste the newly generated words into another document every time I click Run.我的想法是以某种方式使用循环,不断重新分配值,或者每次单击“运行”时将新生成的单词复制/粘贴到另一个文档中。


An example:一个例子:

Randomly Generated Words随机生成的词

Run #1 Output: ['dog', 'cat', 'bag']运行 #1 Output: ['dog', 'cat', 'bag']

Run #2 Output: ['tree', 'hug', 'food']运行 #2 Output: ['tree', 'hug', 'food']

. . . . . .

Run #10 Output: ['hamburger', 'tree', 'desk']运行 #10 Output: ['hamburger', 'tree', 'desk']

This updated CSV file would include ALL words that were randomly generated from the previous times the program was run and look something like this:这个更新后的 CSV 文件将包括所有从之前程序运行时随机生成的单词,看起来像这样:

['dog', 'cat', 'bag', 'tree', 'hug', 'food', ..., 'word_x', 'word_x', 'word_x', ..., 'hamburger', 'tree', 'desk']

(I added 'tree' again to show that repetition is okay for this project) (我再次添加“树”以表明该项目可以重复)


This is what I have so far:这是我到目前为止所拥有的:

Note: This is a reduced version of the data I'm actually using注意:这是我实际使用的数据的简化版本

word_list = ['dog', 'cat', 'tree', 'hungry', 'food', 'hamburger', 'desk', 'bag', 'rain', 'car']
    
rand_word_rep = random.choices(word_list, k=3)
print(rand_word_rep)

Output Output

['dog', 'desk', 'car']

MY QUESTION (Again): If I wanted to make a random word generator that could show me all of the generated words previously produced (in a separate CSV file, for example), how would I do that?我的问题(再次):如果我想制作一个随机单词生成器,它可以向我显示之前生成的所有生成的单词(例如,在单独的 CSV 文件中),我该怎么做?


Hopefully, I was clear in this post.希望我在这篇文章中说得很清楚。 If not, please let me know of your confusion and I'll clarify.如果没有,请让我知道您的困惑,我会澄清。 Thanks in advance!提前致谢!

You can write lines into a file like this:您可以像这样将行写入文件:

with open('data.txt', 'at') as f:
    f.write('this\n')

The 'a' means 'append' and the 't' means 'text'. 'a'表示“追加”, 't'表示“文本”。 Note that you are responsible for writing the lines to the file with the newline character.请注意,您负责使用换行符将行写入文件。

Now you can read this file into a list:现在您可以将此文件读入列表:

with open('data.txt', 'rt') as f:
    data = f.readlines()

Note that the file mode changed to 'rt' for 'read text'.请注意,对于“读取文本”,文件模式已更改为'rt' ”。

The newlines are in there too;换行符也在那里; you can strip them off with str.strip() , for example.例如,您可以使用str.strip()剥离它们。

You can just write to the file normally.您可以正常写入文件。

def write(data, filename):
    with open(filename, 'a') as f:
        f.write('\n'.join(data) + '\n')

def read(filename):
    with open(filename) as f:
        return f.read().splitlines()

def clear(filename):
    with open(filename, 'w'):
        pass

word_list = ['dog', 'cat', 'tree', 'hungry', 'food', 'hamburger', 'desk', 'bag', 'rain', 'car']
    
rand_word_rep = random.choices(word_list, k=3)

write(rand_word_rep, 'chosen_words.txt')

After ['dog', 'cat', 'bag'] , the file would look like:['dog', 'cat', 'bag']之后,文件将如下所示:

dog
cat
bag

After ['tree', 'hug', 'food'] , the file would look like:['tree', 'hug', 'food']之后,文件将如下所示:

dog
cat
bag
tree
hug
food

etc.等等

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

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