-1

I wrote this code, and I have trouble writing the results. The current code only writes the last word, from a file with 50,000 words!

Code:

filename = 'words.txt'

try:
    with open('G:\\Nalpha.txt', 'r') as fileobject:
            contents = fileobject.read()
except FileNotFoundError:
    message = 'Sorry, the file ' + filename + ' connot be found.'
    print(message)
else:
    words = contents.split()
    number_words = len(words)
    print('The file ' + filename + ' has approximatley ' + str(number_words) + ' words.')

alphabet = ('a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z')

unique_words = []
'''Word Frequency'''

words = contents.split()

for word in words:
    if word not in unique_words:
        unique_words.append(word)

    else:
            pass

for i in unique_words:
         word_frequencey = words.count(i)
         print(i, ':', word_frequencey)

textfile = open('G:\\Nalphadone.txt', 'w')
textfile.write(word)
textfile.close()
Charax
  • 41
  • 4
  • As it is, `word` is assigned to the last iteration of your `for` loop – Nicolas Gervais Nov 10 '19 at 12:46
  • Instead of `textfile.write(word)` did you mean `textfile.write(words)` or `textfile.write(word_frequency)`? – Aryerez Nov 10 '19 at 12:50
  • I had tried with `textfile.write(words)` and I got this problem: **TypeError: write() argument must be str, not list**, also I had tried this: `textfile.write(word_frequency)`, and I got: **NameError: name 'word_frequency' is not defined**. – Charax Nov 10 '19 at 12:55
  • It is preferred if you can post separate questions instead of combining your questions into one. That way, it helps the people answering your question and also others hunting for at least one of your questions. – Patrick Artner Nov 10 '19 at 13:08
  • What use has `alphabet` - it is never used - why include it with your [mre]? – Patrick Artner Nov 10 '19 at 13:08
  • To get usique words: `unique = set(words)` – Patrick Artner Nov 10 '19 at 13:09
  • how is your input file structured? 1 word per line? space seperated words in one line? with open('G:\\Nalpha.txt', 'r') as fileobject: `contents = [w.strip() for w in fileobject.readlines()` reads word into a list of lines - no need to split afterwards if you got 1 word per line in your file – Patrick Artner Nov 10 '19 at 13:10
  • Lots of other posts on SO that answer your question - read them, try them, if unclear , ask new and specific question: [count items in list](https://stackoverflow.com/questions/2600191/how-can-i-count-the-occurrences-of-a-list-item), [read excel](https://stackoverflow.com/questions/22169325/read-excel-file-in-python), [write excel](https://stackoverflow.com/questions/13437727/writing-to-an-excel-spreadsheet). Tip - take a look at pandas - it can read in text, count stuff and export to excel - see f.e. https://stackoverflow.com/questions/18936957/count-distinct-words-from-a-pandas-data-frame – Patrick Artner Nov 10 '19 at 13:15
  • The tags you have been using are not appropriate for this question. Please take the [tour], review [what are tags and how should I use them?](//stackoverflow.com/help/tagging). I removed all tags that have currently nothing to do with your code. – Patrick Artner Nov 10 '19 at 13:18

1 Answers1

0

I think the reason you're only getting one element in the .txt is because you open it every time the last for loop cycles. Write mode erases old files with the same name when it is invoked. Try to open the file BEFORE the for loop!