代码之家  ›  专栏  ›  技术社区  ›  Joe Bloggs

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

  •  0
  • Joe Bloggs  · 技术社区  · 7 年前

    我正在使用python字典来比较莎士比亚全集和10000字字典,代码应该将10000字字典中找不到的所有单词输出到一个名为“拼写检查器”的单独文件中。txt'。我相信这段代码中的所有内容都正常运行。在将数据保存到输出文件时,我只遇到一个错误,似乎无法修复它。非常感谢您的帮助。

    错误:

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

    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))    
    
    1 回复  |  直到 7 年前
        1
  •  2
  •   O.Suleiman    7 年前

    只需从 write 方法并将其插入 open 功能如下:

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