python-3.x - UnicodeDecodeError:“utf-8”编解码器无法解码位置 257 中的字节 0x92:无效的起始字节
问题描述
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 行,在 decode (result, used) = self._buffer_decode(data, self.errors, final) UnicodeDecodeError: 'utf-8' codec can't解码位置 257 中的字节 0x92:无效的起始字节”
解决方案
请尝试使用 读取数据encoding='unicode_escape'
。例如:
text_file=open('preprocessing.txt',encoding ='unicode_escape').read()
这为我解决了 UnicodeDecodeError 问题。
否则你可以尝试如下:
text_file=open(r'preprocessing.txt',encoding ='unicode_escape').read()
推荐阅读
- javascript - 动态追加 DIV 并使用 JQuery 中的类名计算 div 内的数据
- docker - Presto 与 Kubernetes
- excel - Outlook VBA 中的错误循环通过收件人
- excel - excel运行时避免打开数据库
- powershell - Send-MailMessage - 无法连接到远程服务器
- mvvmcross - MvvmCross:Android布局绑定字符串资源
- c# - 在反序列化期间使用DataMember,但在序列化期间使用螺母?
- java - 我可以作为制作人删除主题吗?或者我可以在写之前删除主题吗
- peoplesoft - 在 PeopleSoft 中的页面中记录设置
- reactjs - 基于所选用户配置文件语言的默认语言