writing output on file doesn't work in python -


i have code below write out list of n-grams in python.

from nltk.util import ngrams

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

when print out word_grams prints out files correctly when comes writing output files.txt doesn't work. "file.txt" empty.

so guess problem must within these lines of codes:

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

1) final f.close() else want (f inside loop object)

2) name file "file.txt" want output in "files.txt". sure looking in correct file?

3) overwriting file each line in email. perhaps with statement "file.txt" should outside loop.

4) writing "prnt" instead of prnt

something this?

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

Comments

Popular posts from this blog

php - How to add and update images or image url in Volusion using Volusion API -

javascript - jQuery UI Splitter/Resizable for unlimited amount of columns -

javascript - IE9 error '$'is not defined -