2017-09-13 147 views
-2

我試圖使用python從csv文件中的列創建文字雲。我已經安裝了wordcloud,PIL,OS和Numpy,但我不確定如何只讀這一列來創建詞雲。有人能夠協助嗎?謝謝!從CSV文件的列中創建Python中的文字雲

+1

歡迎的話,請澄清你的問題,告訴你已經嘗試過什麼,你做了什麼輸出/錯誤,我們可以試着幫 – Scriptable

+1

這個問題不不符合StackOverflow的規則,因爲它太寬泛,只會吸引基於意見的答案(所以期望它被關閉)。請轉到:http://stackoverflow.com/help,然後閱讀:http://stackoverflow.com/help/on-topic和:http://stackoverflow.com/help/how-to-ask。 –

回答

0

這是來自word_cloud site的修改示例。

我有一個'words.csv文件,在第一列中有文字(在下面的圖片中選擇並顯示)和他們在下一個文件中的解釋。

我們讀csv文件(單詞)的第一列,然後通過word_cloud運行。

import csv 
from wordcloud import WordCloud 


#read first column of csv file to string of words seperated 
#by tab 

your_list = [] 
with open('words.csv', 'rb') as f: 
    reader = csv.reader(f) 
    your_list = '\t'.join([i[0] for i in reader]) 


# Generate a word cloud image 
wordcloud = WordCloud().generate(your_list) 

# Display the generated image: 
# the matplotlib way: 
import matplotlib.pyplot as plt 
plt.imshow(wordcloud, interpolation='bilinear') 
plt.axis("off") 

# lower max_font_size 
wordcloud = WordCloud(max_font_size=40).generate(your_list) 
plt.figure() 
plt.imshow(wordcloud, interpolation="bilinear") 
plt.axis("off") 
plt.show() 

# The pil way (if you don't have matplotlib) 
# image = wordcloud.to_image() 
# image.show() 

圖片
enter image description here