简体   繁体   English

Python字典拼写检查器,“ TypeError:write()不带关键字参数”

[英]Python Dictionary Spell Checker, 'TypeError: write() takes no keyword arguments'

I'm using a python dictionary to compare Shakespeares full works and a 10,000 word dictionary, the code should output all words which weren't found in the 10,000 word dictionary to a separate file called 'SpellChecker.txt'. 我正在使用python词典比较莎士比亚的完整著作和10,000个单词的词典,该代码应将10,000个单词的词典中找不到的所有单词输出到名为'SpellChecker.txt'的单独文件中。 I believe everything in this code is running correctly. 我相信这段代码中的所有内容都可以正常运行。 I am only coming across one error to do with saving the data to the output file and can't seem to fix it. 我只遇到一个错误,该错误与将数据保存到输出文件有关,似乎无法修复。 Any help is appreciated. 任何帮助表示赞赏。

Error: 错误:

Traceback (most recent call last):
  File "/Users/JakeFrench/Desktop/HashTable.py", line 29, in <module>
    f1.write(word+'\n', encoding= 'utf-8')

TypeError: write() takes no keyword arguments TypeError:write()不带关键字参数

import re
import time
start_time = time.time()

f1=open ('SpellChecker.txt', 'w+')

Dictionary = {}
Document = []
with open ('10kWords.txt', encoding= 'utf-8') as f:
        for word in f:
            Dictionary[word.rstrip()] = 1

with open ('ShakespeareFullWorks.txt', encoding= 'utf-8') as f:
    content = f.read().split(" ")
    content = [item.lower() for item in content]
    content = ' '.join(content)
    content = re.findall("\w+", content)
    for line in content:
        Document.append(line)

for line in content:
    for word in line.split():
        if word.lower() not in Dictionary:
            f1.write(word+'\n', encoding= 'utf-8')

f1.close()            

print ("--- %s seconds ---" % (time.time() - start_time))    

Just remove the encoding attribute from the write method and insert it in the open function as follows: 只需从write方法中删除encoding属性,然后将其插入到open函数中,如下所示:

f1=open ('SpellChecker.txt', 'w+', encoding='utf-8')
  ...
f1.write(word+'\n')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM