From 8202de20a52cb64bcc3525dcea9f5ed25affbc2b Mon Sep 17 00:00:00 2001 From: pzb7h6yxf <1736289433@qq.com> Date: Tue, 17 Sep 2024 10:02:08 +0800 Subject: [PATCH] =?UTF-8?q?=E5=B0=86=E7=BB=9F=E8=AE=A1=E7=9A=84=E6=95=B0?= =?UTF-8?q?=E6=8D=AE=E5=88=A9=E7=94=A8=E7=BC=96=E7=A8=8B=E5=B7=A5=E5=85=B7?= =?UTF-8?q?=E8=87=AA=E5=8A=A8=E5=86=99=E5=85=A5Excel=E8=A1=A8=E4=B8=AD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- 2.2.2.py | 41 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) create mode 100644 2.2.2.py diff --git a/2.2.2.py b/2.2.2.py new file mode 100644 index 0000000..98d80af --- /dev/null +++ b/2.2.2.py @@ -0,0 +1,41 @@ +import jieba +import wordcloud +import imageio +import pandas as pd # 用于读取Excel文件 + +# 读取图片作为词云的形状 +img = imageio.imread('四叶草1.png') + +# 使用pandas读取Excel文件中的所有列 +df = pd.read_excel('top_ai_danmakus.xlsx') + +# 将Excel所有列的数据拼接成一个长字符串 +book = '' + +# 遍历每一列并将内容合并成字符串 +for column in df.columns: + # 将当前列转换为字符串并拼接到 book 变量 + book += ' '.join(df[column].astype(str)) + ' ' + +# 使用jieba进行分词 +book_list = jieba.lcut(book) +book_str = ' '.join(book_list) + +# 打印分词后的字符串 +print(book_str) + +# 创建词云 +wc = wordcloud.WordCloud( + width=500, + height=500, + background_color='white', + mask=img, + stopwords={'main','Taipei','afraid','aiden','Britain'}, # 可添加不需要的停用词 + font_path='msyh.ttc' # 设置中文字体路径 +) + +# 生成词云 +wc.generate(book_str) + +# 保存词云图 +wc.to_file('词云图.png')