简体   繁体   English

在文件上写输出在python中不起作用

[英]writing output on file doesn't work in python

I have the code below to write out a list of N-grams in Python. 我有下面的代码来写出Python中的N-gram列表。

from nltk.util import ngrams 从nltk.util导入ngrams

def word_grams(words, min=1, max=6):
    s = []
    for n in range(min, max):
        for ngram in ngrams(words, n):
            s.append(' '.join(str(i) for i in ngram))
    return s
email = open("output.txt", "r")
for line in email.readlines():
    with open('file.txt', 'w') as f:
            for line in email:
                prnt = word_grams(email.split(' '))
                f.write("prnt")
email.close()
f.close()

when I print out the word_grams it prints out the files correctly but when it comes to writing the output into files.txt it doesn't work. 当我打印出word_grams它可以正确打印出文件,但是当将输出写入files.txt它不起作用。 The "file.txt" is empty. “ file.txt”为空。

So I guess the problem must be within these lines of codes: 所以我想问题一定在这些代码行之内:

for line in email.readlines():
    with open('file.txt', 'w') as f:
            for line in email:
                prnt = word_grams(email.split(' '))
                f.write("prnt")
email.close()
f.close()

1) the final f.close() does something else than what you want (f inside the loop is another object) 1)最后的f.close()做的事情比您想要的要多(循环内的f是另一个对象)

2) You name the file "file.txt" but want the output in "files.txt". 2)您将文件命名为“ file.txt”,但要在“ files.txt”中输出。 Are you sure that you are looking in a correct file? 您确定要查找正确的文件吗?

3) You are overwriting the file for each line in the email. 3)您正在覆盖电子邮件中每一行的文件。 Perhaps the with statement for "file.txt" should be outside the loop. 也许“ file.txt”的with语句应该在循环之外。

4) You are writing "prnt" instead of prnt 4)您写的是"prnt"而不是prnt

Something like this? 像这样吗

def word_grams(words, min=1, max=6):
    s = []
    for n in range(min, max):
        for ngram in ngrams(words, n):
            s.append(' '.join(str(i) for i in ngram))
    return s

with open("output.txt", "r") as email:
    with open('file.txt', 'w') as f:
        for line in email.readlines():
            prnt = word_grams(line.split(' '))
            for ngram in prnt:
                f.write(ngram)

I don't know what you are trying to accomplish exactly, but it seems that you would like to apply the function word_grams to every word in the file "output.txt" and save the output to a file called "file.txt", probably one item per line. 我不知道您要确切地完成什么,但是似乎您想将word_grams函数word_grams文件“ output.txt”中的每个单词,并将输出保存到名为“ file.txt”的文件中,每行大概一个项目。

With these assumptions, I would recommend to rewrite your iteration in this manner: 基于这些假设,我建议以这种方式重写您的迭代:

words = []
# load words from input
with open("output.txt") as f:
    for line in f:
        words += line.strip().split(" ")
# generate and save output
grams = apply(word_grams, words)
with open("file.txt", "w") as f:
    f.write("\n".join(grams))

However, this code assumes that the function word_grams is working properly. 但是,此代码假定word_grams函数正常工作。

Your code in loop: 您的循环代码:

for line in email:

did not run! 没有跑!

Because after email.readlines() run,the variable email is empty. 因为在运行email.readlines()之后,变量email为空。 You can do some test like fallows: 您可以进行一些测试,例如:

email = open("output.txt", "r")
for line in email.readlines():
    print '1'
    for line in email:
        print '2'

if you have 3 lines in your output.txt,after you run this test,you will get: 如果output.txt中有3行,运行此测试后,您将获得:

1
1
1

in the output. 在输出中。

And you can do a test like this: 您可以进行如下测试:

email = open("output.txt", "r")
email.readlines()

you will see a list with the lines in your output.txt. 您将在output.txt中看到包含各行的列表。

but when you run email.readlines() again,you will get an empty list! 但是当您再次运行email.readlines()时,将得到一个空列表!

so,there should be the problem.your variable email is empty in your second loop. 因此,应该是有问题的。您的可变email在第二个循环中为空。

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

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