问题描述
以下是我的代码:
def byFreq(pair):
return pair [1]
def ratio(file):
#表征作者并构建一个字典
text = open(file,'r')。read()。lower()
#text + = open(file2,'r')。read()。lower()
#text + = open(file3,'r')。 in'!#$%&()* +, - 。/:;< =>?@ [\\] ^ _`{|}〜':
text = text.replace (ch,'')
words = text.split()
构造一个词计数字典
计数= {}
wordNum = 0
for w in words:
计数[w] = counts.get(w,0)+ 1
wordNum = wordNum + 1
#print(此处的单词总数文本是,wordNum)
输出分析n个最常见的单词
n = 50
items = list(counts.items())
items.sort ()
items.sort(key = byFreq,reverse = True)
#print(独特的数量rds in,file,is,len(计数),。)
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()
我收到以下错误:
追溯(最近的call last):
文件F:/ PyCharm /难度项目测试/ dictionarygenerator.py,第48行< module>
main()
文件F:/ PyCharm /困难项目测试/ dictionarygenerator.py,第43行,主要
text = open(outfile,'w')。write()
TypeError:write()只需要一个参数(0)
任何人都可以告诉我我做错了什么,如何解决它,因为我无法弄清楚。任何帮助将不胜感激,谢谢。
这里有两个问题。
首先,您第一次拨打 write()
时不会写任何内容。只需要调用一次。
text = open(outfile,'w')
text.write (melvile)
您需要告诉文本
对象在打开文件写入后要写的内容。
第二, melville
不是一个字符串。您可以打印字典,假设您只想将值打印到文本文件中。
text = open(outfile,'w ')
pre>
为melville中的键:
text.write(%s:%f\\\
,key,melville [key])
The following is my code:
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()
I keep getting the following error:
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)
Can anyone tell me what I am doing wrong and how to fix it because I can't figure it out. Any assistance will be much appreciated, thank you.
解决方案You have two problems here.
First, you aren't writing anything the first time you call
write()
. It is only necessary to call it once.text = open(outfile, 'w') text.write(melvile)
You need to tell the
text
object what to write after you open the file for writing.Second,
melville
isn't a string. You can print the dictionary assuming you just want to print the values to a text file.text = open(outfile, 'w') for key in melville: text.write("%s: %f\n", key, melville[key])
这篇关于将字典写入文本文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!