Note that there are some explanatory texts on larger screens.

plurals
  1. POWhy did my method of writing list items to a .txt file not work?
    primarykey
    data
    text
    <p>I've written a short program will take an input file, remove punctuation, sort the contents by number of occurrences per word and then write the 100 most common results to an output file.</p> <p>I had some trouble on the last part (writing the results to an output file), and though I've fixed it, I don't know what the problem was.</p> <p>The full code looks like so:</p> <pre><code>from collections import Counter from itertools import chain import sys import string wordList = [] #this file contains text from a number of reviews file1 = open('reviewfile', 'r+') reviewWords = file1.read().lower() #this file contains a list of the 1000 most common English words file2 = open('commonwordsfile', 'r') commonWords = file2.read().lower() #remove punctuation for char in string.punctuation: reviewWords = reviewWords.replace(char, " ") #create a list of individual words from file1 splitWords = reviewWords.split() for w in splitWords: if w not in commonWords and len(w)&gt;2: wordList.append(w) #sort the resulting list by length wordList = sorted(wordList, key=len) #return a list containing the 100 #most common words and number of occurrences words_to_count = (word for word in wordList) c = Counter(words_to_count) commonHundred = c.most_common(100) #create new file for results and write #the 100 most common words to it fileHandle = open("outcome", 'w' ) for listItem in commonHundred: fileHandle.write (str(listItem) + "\n") fileHandle.close() </code></pre> <p>I previously had this following code snippet attempting to write the 100 most common terms to a .txt file, but it didn't work. Can anyone explain why not?</p> <pre><code>makeFile = open("outputfile", "w") for item in CommonHundred: makeFile.write("[0]\n".format(item)) makeFile.close() </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload