pip install wordcloud
使用 pip本地安裝wordcloud文件解決c++環(huán)境缺失的問題
使用方式,在工作目錄下wordcloud.whl文件
打開終端 pip install [文件路徑]文件名
pip install wordcloud.whl
pip install matplotlib
導(dǎo)入英文
實例:
from wordcloud import WordCloud
text = """Alice in Wonderland, how do you get to Wonderland?
Over the hill or underland, or just behind the tree?
When clouds go rolling by, they roll away and leave the sky.
Where is the land beyond the eye, the people can not see, where can it be?
Where do stars go, where is the grass that's blue?
They must be somewhere in the sunny afternoon.
Alice in Wonderland, where is the path to Wonderland?
Over the hill or here or there, I wonder where."""
wc = WordCloud().generate(text)
wc.to_file('Alice.png')
導(dǎo)入中文
實例:
from wordcloud import WordCloud
text ="""天地靈氣孕育出一顆能量巨大的混元珠不铆,元始天尊將混元珠提煉成靈珠和魔丸讽营,靈珠投胎為人憔恳,助周伐紂時可堪大用齿尽;而魔丸則會誕出魔王哨苛,為禍人間均蜜。元始天尊啟動了天劫咒語但绕,3年后天雷將會降臨,摧毀魔丸扶供。太乙受命將靈珠托生于陳塘關(guān)李靖家的兒子哪吒身上筛圆。然而陰差陽錯,靈珠和魔丸竟然被掉包椿浓。本應(yīng)是靈珠英雄的哪吒卻成了混世大魔王太援。調(diào)皮搗蛋頑劣不堪的哪吒卻徒有一顆做英雄的心。然而面對眾人對魔丸的誤解和即將來臨的天雷的降臨扳碍,哪吒是否命中注定會立地成魔提岔?他將何去何從?"""
# 調(diào)用WordCloud類 生成詞云對象
wc = WordCloud(
background_color = 'White',
font_path = 'msyh.ttc', # 微軟雅黑字體
width = 800,
height = 600
).generate(text)
wc.to_file('Nazha.png')
實例:
import jieba
from wordcloud import WordCloud
text ="""天地靈氣孕育出一顆能量巨大的混元珠笋敞,元始天尊將混元珠提煉成靈珠和魔丸碱蒙,靈珠投胎為人,助周伐紂時可堪大用夯巷;而魔丸則會誕出魔王赛惩,為禍人間。元始天尊啟動了天劫咒語趁餐,3年后天雷將會降臨喷兼,摧毀魔丸。太乙受命將靈珠托生于陳塘關(guān)李靖家的兒子哪吒身上后雷。然而陰差陽錯季惯,靈珠和魔丸竟然被掉包。本應(yīng)是靈珠英雄的哪吒卻成了混世大魔王臀突。調(diào)皮搗蛋頑劣不堪的哪吒卻徒有一顆做英雄的心勉抓。然而面對眾人對魔丸的誤解和即將來臨的天雷的降臨,哪吒是否命中注定會立地成魔候学?他將何去何從琳状?"""
# 中文分詞處理
txt_list = jieba.lcut(text)
# 將列表轉(zhuǎn)化成字符串,用空格隔開
txt = " ".join(txt_list)
print(txt)
# 調(diào)用WordCloud類 生成詞云對象
wc = WordCloud(
background_color = 'White',
font_path = 'msyh.ttc', # 微軟雅黑字體
width = 800,
height = 600
).generate(txt)
wc.to_file('Nezha1.png')
python中的文件操作
讀取文件 open()內(nèi)置函數(shù)可以讀取指定路徑的文件
file是文件的路徑,mode = 'r' 是以只讀方式打開
實例
f = open(file="text,txt",mode='r',encoding='UTF-8')
txt = f.read()
f.close()
print(txt)
# 我來到東軟學(xué)習(xí)python
使用with 上下文管理器進(jìn)行文件讀取
實例:
with open('text,txt','r',encoding=('UTF-8')) as f:
print(f.read())
# 我來到東軟學(xué)習(xí)python