代码: import jieba import wordcloud #1、读取文件 with open('政府工作报告.txt') as f: txt = f.read() #字符串的数据类型 #print(txt) #2、分词操作 lst = jieba.lcut(txt) print(lst) #3、统计列表中所有的元素的个数 dic = {word: lst.count(word) for word in lst} print(dic) #5、去掉停用词 with open('中文虚词列表.txt') as f: stop = eval(f.read()) for i in list(dic.keys()): if i in stop: dic.pop(i) print(dic) #6、制作词云图 import matplotlib.pyplot as plt img = plt.imread('ditu.jpg') hb = wordcloud.WordCloud(background_color='red', width=800, height=800, font_path='STXINGKA.TTF', mask=img) hb.generate_from_frequencies(dic) hb.to_file('aaaaaaaa.jpg')
报错:
Traceback (most recent call last):
File "/Users/elena/PycharmProjects/python_Project/5.23.py", line 81, in <module>
import wordcloud
File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/wordcloud/__init__.py", line 1, in <module>
from .wordcloud import (WordCloud, STOPWORDS, random_color_func,
File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/wordcloud/wordcloud.py", line 19, in <module>
import matplotlib
File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/matplotlib/__init__.py", line 109, in <module>
from . import _api, _version, cbook, docstring, rcsetup
File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/matplotlib/cbook/__init__.py", line 31, in <module>
from matplotlib import _api, _c_internal_utils
ImportError: dlopen(/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/matplotlib/_c_internal_utils.cpython-310-darwin.so, 0x0002): tried: '/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/matplotlib/_c_internal_utils.cpython-310-darwin.so' (mach-o file, but is an incompatible architecture (have 'arm64', need 'x86_64')), '/usr/lib/_c_internal_utils.cpython-310-darwin.so' (no such file)