简体   繁体   中英

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'. 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

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:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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