首页 > 解决方案 > 为什么长文本文件在 Spyder 中不起作用?

问题描述

我正在开发一个需要长文本文件(键盘密码解码器)的程序,并且正在使用 Spyder。它表示无法检索某个位置的内存。我尝试将文本文件和程序放在同一个文件夹中。这是 spyder 本身的问题还是我的代码的问题?对于上下文,.txt 文件有大约 500,000 个字符(100,000 个最常用的单词)。

with open("wiki-100k.txt") as word_file:
    english_words = set(word.strip().lower() for word in word_file)

def IsWord(word):
    return word.lower() in english_words

print(IsWord("HaMs"))

我收到此错误:

Traceback (most recent call last):

  File "C:\Users\eirta\Desktop\Ciphers\Keypad.py", line 3, in <module>
    english_words = set(word.strip().lower() for word in word_file)

  File "C:\Users\eirta\Desktop\Ciphers\Keypad.py", line 3, in <genexpr>
    english_words = set(word.strip().lower() for word in word_file)

  File "C:\Users\eirta\anaconda3\lib\encodings\cp1252.py", line 23, in decode
    return codecs.charmap_decode(input,self.errors,decoding_table)[0]

UnicodeDecodeError: 'charmap' codec can't decode byte 0x8d in position 2814: character maps to <undefined>

标签: pythonpython-3.xspyder

解决方案


推荐阅读