python问题,我运用python做中文词频分析的时候总是显示UnicodeDecodeError: 'utf-8'问题?
以下是我在python3.7idle中写的的语句importjiebatxt=open("E:/study/pythondata/应用资料/三国演义.txt","r",e...
以下是我在python3.7 idle中写的的语句
import jieba
txt=open("E:/study/python data/应用资料/三国演义.txt","r",encoding="utf-8").read()
words=jieba.lcut(txt)
counts={}
for word in words:
if len(word)==1:
continue
else:
counts[word]=counts.get(word,0)+1
items=list(counts.items())
items.sort(key=lambda x:x[1],reverse=True)
n=eval(input("前几位中文分词?"))
for i in range(n):
word,count=items[i]
print("{0:<10}{1:>5}".format(word,count))
下面是反馈出来的问题,求解救
Traceback (most recent call last):
File "E:\study\python data\小程序\汉语文章分词数量分析.py", line 2, in <module>
txt=open("E:/study/python data/应用资料/三国演义.txt","r",encoding="utf-8").read()
File "C:\Users\***\AppData\Local\Programs\Python\Python37\lib\codecs.py", line 322, in decode
(result, consumed) = self._buffer_decode(data, self.errors, final)
UnicodeDecodeError: 'utf-8' codec can't decode byte 0xc8 in position 0: invalid continuation byte 展开
import jieba
txt=open("E:/study/python data/应用资料/三国演义.txt","r",encoding="utf-8").read()
words=jieba.lcut(txt)
counts={}
for word in words:
if len(word)==1:
continue
else:
counts[word]=counts.get(word,0)+1
items=list(counts.items())
items.sort(key=lambda x:x[1],reverse=True)
n=eval(input("前几位中文分词?"))
for i in range(n):
word,count=items[i]
print("{0:<10}{1:>5}".format(word,count))
下面是反馈出来的问题,求解救
Traceback (most recent call last):
File "E:\study\python data\小程序\汉语文章分词数量分析.py", line 2, in <module>
txt=open("E:/study/python data/应用资料/三国演义.txt","r",encoding="utf-8").read()
File "C:\Users\***\AppData\Local\Programs\Python\Python37\lib\codecs.py", line 322, in decode
(result, consumed) = self._buffer_decode(data, self.errors, final)
UnicodeDecodeError: 'utf-8' codec can't decode byte 0xc8 in position 0: invalid continuation byte 展开
1个回答
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询