from wordcloud import WordCloud
import jieba
import numpy as np
import collections
import matplotlib.pyplot as plt
from PIL import Image
with open('西游记.txt','rb') as f:
data = f.read()
seg_list = jieba.cut(data,cut_all=True)
with open('停用词.txt','rb') as f:
con = f.readlines()
stop_words = set()
for i in con:
i = i.replace("\n","")
stop_words.add(i)
result_list =[]
for word in seg_list:
if word not in stop_words and len(word)>1:
result_list.append(word)
word_counts = collections.Counter(result_list)
word_counts_top100 = word_counts.most_common(100)
print(word_counts_top100)
img=Image.open('孙悟空.jfif')
img = np.array(img)
my_cloud = WordCloud(
background_color='white',
width=900,height=600,
max_words=100,
font_path='msyh.ttc',
max_font_size=99,
min_font_size=16,
random_state=50,
mask=img
).generate_from_frequencies(word_counts)
plt.imshow(my_cloud)
plt.axis('off')
plt.show()
求其中
Traceback (most recent call last):
File "D:\study\cscscs\main.py", line 14, in
i = i.replace("\n","")
TypeError: a bytes-like object is required, not 'str'
的解决办法