以下是我的代码:
def byFreq(pair):
return pair[1]
def ratio(file):
#characterizes the author and builds a dictionary
text = open(file,'r').read().lower()
# text += open(file2,'r').read().lower()
# text += open(file3,'r').read().lower()
for ch in '!"#$%&()*+,-./:;<=>?@[\\]^_`{|}~':
text = text.replace(ch, ' ')
words = text.split()
"construct a dictionary of word counts"
counts = {}
wordNum = 0
for w in words:
counts[w] = counts.get(w, 0) + 1
wordNum = wordNum + 1
# print ("The total number of words in this text is ",wordNum)
"output analysis of n most frequent words"
n = 50
items = list(counts.items())
items.sort()
items.sort(key=byFreq, reverse=True)
# print("The number of unique words in", file, "is", len(counts), ".")
r = {}
for i in range(n):
word, count = items[i]
"count/wordNum = Ratio"
r[word] = (count/wordNum)
return r
def main():
melvile = ratio("MelvilleText.txt")
print(melvile)
outfile = input("File to save to: ")
text = open(outfile, 'w').write()
text.write(melvile)
main()
我不断收到以下错误:
Traceback (most recent call last):
File "F:/PyCharm/Difficult Project Testing/dictionarygenerator.py", line 48, in <module>
main()
File "F:/PyCharm/Difficult Project Testing/dictionarygenerator.py", line 43, in main
text = open(outfile, 'w').write()
TypeError: write() takes exactly 1 argument (0 given)
谁能告诉我我做错了什么以及如何解决它,因为我无法弄清楚。任何帮助将不胜感激,谢谢。