m0_75130226 2023-01-10 23:17 采纳率: 50%
浏览 13
已结题

a bytes-like object is required, not 'str'


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'
的解决办法

  • 写回答

1条回答 默认 最新

  • williamwht 2023-01-11 08:16
    关注

    i.replace("\n", "")改为i.replace(b"\n", b"")或者i.replace('\n', '')即可

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

问题事件

  • 系统已结题 1月19日
  • 已采纳回答 1月11日
  • 创建了问题 1月10日

悬赏问题

  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题