【问题标题】:UnicodeDecodeError: 'utf-8' codec can't decode byte 0x92 in position 257: invalid start byteUnicodeDecodeError:“utf-8”编解码器无法解码位置 257 中的字节 0x92:无效的起始字节
【发布时间】:2019-09-13 07:48:58
【问题描述】:
I am new in python and want to apply p reprocessing steps 
so here is decoding error 

import nltk
from nltk.tokenize import word_tokenize,sent_tokenize
from nltk.corpus import stopwords
from nltk.tag import pos_tag
from nltk.stem import PorterStemmer

`ps=PorterStemmer()
print ("\n Reading file with out stopwords.")
text_file=open('preprocessing.txt',encoding='utf-8').read()
stop_words= set(stopwords.words("english"))
words=word_tokenize(text_file)
filtered_sentence = [w for w in words if not w in stop_words]
print(filtered_sentence)
print ("\n Removed stopword.")
print(stop_words)
print ("\n Stemming.")
for w in text_file:
print (ps.stem(w))
print(w)
print(sent_tokenize(text_file))
print ("\n tokenization.")
print(word_tokenize(text_file))
print ("\n part of speech tagging.")
print (pos_tag(words))   `

" 我想以特定格式显示结果,但输出是 ",第 322 行,在解码中 (结果,消耗)= self._buffer_decode(数据,self.errors,最终) UnicodeDecodeError:“utf-8”编解码器无法解码位置 257 中的字节 0x92: 无效的起始字节”

【问题讨论】:

  • 您确定您的文件是使用 UTF-8 编码的吗?
  • 没有怎么编码?
  • 如果你不确定你的文件有什么编码,你可以试试chardet来弄明白。

标签: python-3.x


【解决方案1】:

请尝试使用encoding='unicode_escape' 读取数据。例如:

text_file=open('preprocessing.txt',encoding ='unicode_escape').read()

这为我解决了 UnicodeDecodeError。

否则你可以尝试如下:

text_file=open(r'preprocessing.txt',encoding ='unicode_escape').read()

【讨论】:

    【解决方案2】:

    确保您的文件使用 UTF-8 编码。如果没有,请在 Notepad++ 中打开它,转到编码选项卡,然后转换为 UTF-8 并另存为。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-02-10
      • 2020-06-22
      • 1970-01-01
      • 1970-01-01
      • 2022-05-30
      • 2021-12-01
      • 2016-05-13
      相关资源
      最近更新 更多