?wmq 2021-03-17 14:46 采纳率: 0%
浏览 186
已结题

怎么用jupyter生成wordcloud中文图

df = pd.read_excel (r'C:\Users\Desktop\train.xlsx', sheet_name='SunValley')
print (df.head(10))

from wordcloud import WordCloud
text = open(df,'r',encoding='utf-8').read()
wc =WordCloud(
    max_font_size=110, 
    max_words=100, 
    font_path='C:\Windows\Fonts\simkai.ttf', 
    background_color="white",
    width=800,
    height=500,
    random_state = 20).generate(str(text))

plt.figure(figsize=(10, 7))
plt.imshow(wordcloud, interpolation="bilinear")
plt.axis('off')
plt.savefig("Wordcloud for comments.png", dpi = 300)
plt.show()
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-30-710a292a210d> in <module>
      1 from wordcloud import WordCloud
----> 2 text = open(df,'r',encoding='utf-8').read()
      3 wc =WordCloud(
      4     max_font_size=110,
      5     max_words=100,

TypeError: expected str, bytes or os.PathLike object, not DataFrame

我的excel文档是中文的,想知道哪里错了,怎么改正?谢谢!!

 

  • 写回答

2条回答 默认 最新

  • PythonJavaC++go 2021-03-17 15:36
    关注

    不需要df = pd.read_excel (r'C:\Users\Wang Meiqi\Desktop\train.xlsx', sheet_name='SunValley') 这个

    把excel文件的内容导出成txt文件(比如导出为r'C:\Users\Wang Meiqi\Desktop\train.txt');

    然后 text = open(r'C:\Users\Wang Meiqi\Desktop\train.txt','r',encoding='utf-8').read()

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 c程序不知道为什么得不到结果
  • ¥40 复杂的限制性的商函数处理
  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置