Andrew Ravus
Andrew Ravus

Reputation: 461

writing output on file doesn't work in python

I have the code below to write out a list of N-grams in Python.

from nltk.util import 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. The "file.txt" is empty.

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()

Upvotes: 0

Views: 102

Answers (3)

hxysayhi
hxysayhi

Reputation: 1987

Your code in loop:

for line in email:

did not run!

Because after email.readlines()run,the variable email is empty. 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:

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.

but when you run email.readlines()again,you will get an empty list!

so,there should be the problem.your variable email is empty in your second loop.

Upvotes: 0

Sven Rusch
Sven Rusch

Reputation: 1377

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.

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.

Upvotes: 1

honza_p
honza_p

Reputation: 2093

1) the final f.close() does something else than what you want (f inside the loop is another object)

2) You name the file "file.txt" but want the output in "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. Perhaps the with statement for "file.txt" should be outside the loop.

4) You are writing "prnt" instead of 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)

Upvotes: 1

Related Questions