Python3解决问题:编写词云代码,总是报错

Python3解决问题:编写词云代码,总是报错

代码

#GovRptWordCloudv1.py
import jieba
import wordcloud
f = open(“gr.txt”, “r”,encoding=“utf-8”)
t = f.read()
f.close()
ls = jieba.lcut(t)

txt = " ".join(ls)
w = wordcloud.WordCloud(
width = 1000, height = 700,
background_color = “white”,
font_path = “msyh.ttc”
)
w.generate(txt)
w.to_file(“grwordcloud.png”)

报错

Traceback (most recent call last):
File “C:\Users\lenovo\AppData\Local\Programs\Python\Python38-32\GovRptWordCloudv1.py”, line 5, in
t = f.read()
File “C:\Users\lenovo\AppData\Local\Programs\Python\Python38-32\lib\codecs.py”, line 322, in decode
(result, consumed) = self._buffer_decode(data, self.errors, final)
UnicodeDecodeError: ‘utf-8’ codec can’t decode byte 0xa1 in position 0: invalid start byte

原因

有出现同样情况的吗?请问下怎么解决呢?谢谢啦,编程小白弄了一下午了,就是找不出错误

你可能感兴趣的:(Python3解决问题:编写词云代码,总是报错)