类型错误:预期的字符串或类似字节的对象(Python 3)(Wordcloud)

2023-12-28

import pandas as pd
import matplotlib.pyplot as plt
from PIL import Image
import numpy as np
import wordcloud
from wordcloud import WordCloud,STOPWORDS

# Read the whole text.
remarks = open(r'C:\Users\marmar\Remarks.txt').read().split()
#Create words over an image
mask = np.array(Image.open(r'C:\users\marmar\Documents\cloud.png'))

 #set the stopwords list
 stopwords= set(STOPWORDS)
 #append new words to the stopwords list
 new_words =open(r'C:\Users\marmar\Documents\comments.txt').read().split()
 new_stopwords=stopwords.union(new_words)
 #generate the word cloud with parameters
wc = 
   WordCloud(
         background_color="white", max_words=2000, 
         mask=mask,min_font_size =15, max_font_size=40, relative_scaling = 
         0.5, stopwords=new_stopwords,normalize_plurals= True)
wc.generate(remarks)
plt.figure(figsize=(25,25))
plt.imshow(wc, interpolation="bilinear")
plt.axis("off")

#Show the wordcloud
plt.show()

所以,如果我删除.read() .split()从我的备注文本文件中,它实际上可以工作并返回词云。但是,我希望能够对单词进行标记,并且实际上使词云准确。 (它没有显示单词分开)。但是,每次我这样做时,都会收到此错误。

TypeError                                 Traceback (most recent call last)
<ipython-input-7-76f0df420fc2> in <module>()
 19 wc = WordCloud(background_color="white", max_words=2000, 
 mask=mask,min_font_size =15, max_font_size=40, relative_scaling = 0.5, 
 stopwords=new_stopwords,
 20                 normalize_plurals= True)
 ---> 21 wc.generate(remarks)
 22 plt.figure(figsize=(25,25))
 23 plt.imshow(wc, interpolation="bilinear")

 ~\AppData\Local\Continuum\anaconda3\lib\site-
 packages\wordcloud\wordcloud.py in generate(self, text)
  563         self
 564         """
 --> 565         return self.generate_from_text(text)
 566 
 567     def _check_generated(self):

 ~\AppData\Local\Continuum\anaconda3\lib\site-
packages\wordcloud\wordcloud.py in generate_from_text(self, text)
544         self
545         """
--> 546         words = self.process_text(text)
547         self.generate_from_frequencies(words)
548         return self

~\AppData\Local\Continuum\anaconda3\lib\site-packages\wordcloud\wordcloud.py 
in process_text(self, text)
511         regexp = self.regexp if self.regexp is not None else r"\w[\w']+"
512 
--> 513         words = re.findall(regexp, text, flags)
514         # remove stopwords
515         words = [word for word in words if word.lower() not in 
stopwords]

~\AppData\Local\Continuum\anaconda3\lib\re.py in findall(pattern, string, 
flags)
220 
221     Empty matches are included in the result."""
--> 222     return _compile(pattern, flags).findall(string)
223 
224 def finditer(pattern, string, flags=0):

TypeError: expected string or bytes-like object

我试图做的实际上是从我的备注文本文件中删除所有标点符号,并将文本文件转换为 unicode。评论文本文件工作正常,不知道为什么我的备注文件不能。

Thanks!


None

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

类型错误:预期的字符串或类似字节的对象(Python 3)(Wordcloud) 的相关文章

随机推荐

  • PHP cli 命令行 safe_mode 限制

    我正在使用 Kohana 框架 3 0 9 它生成每日日志 如果日志文件是在 CRON 运行脚本的前一天制作的 我想邮寄日志文件 但经过几天的尝试 我无法弄清楚如何在 PHP CLI 模式中推迟 safe mode 当我在网络上运行脚本时
  • Android任务管理器源码

    我想知道是否有任何开源 Android 任务管理器 流程管理器 流程细节器 项目可供我查看 我正在为我的大学项目开发 一个应用程序 它将帮助我了解寻找一个好的任务管理器 有谁知道一些开源项目 我可以在其中阅读或查看源代码 我不需要下载源 谢
  • Rails,选择助手,添加样式

    我正在尝试做这样的事情 select model attribute style gt some style Add style to the select helper在 Rails 中 但它不起作用 来自文档 http api ruby
  • 有条件申请课程的最佳方式是什么?

    假设您有一个在 a 中呈现的数组ul与li对于每个元素和控制器上的一个属性 称为selectedIndex 将课程添加到的最佳方式是什么li与索引selectedIndex在 AngularJS 中 我目前正在 手工 复制li代码并将该类添
  • 我如何像 Cocos2D 一样通过 SpriteKit 倾斜/剪切精灵?

    在Cocos2D x中 CCNode类提供了 skewX 和 skewY 来让我对精灵进行一些扭曲 但是 我在SpriteKit的SKNode中找不到类似的映射 我的游戏使用Flash移植骨骼动画 其中精灵的定位 缩放 旋转和剪切的配置将被
  • WebSocket 服务器不支持 SSL

    我有一个使用网络套接字的工作聊天应用程序 我想更进一步 在我的连接上启用加密 但是当我用 https 服务器切换 http 服务器时 我的连接开始失败 我已经生成了一个在我的所有网站上使用的自签名证书 在同一 TLD 下 这意味着它是通配符
  • 二元运算符“||”的操作数类型错误- 爪哇

    我在一个项目上遇到了麻烦 而且我没有足够的词汇来搜索我遇到的问题 我认为这与Java有关字符的语法有关 除了下面的代码之外 输入被视为上面的字符串并解析为字符 switch accountType case c case C Determi
  • 如何从 Rails 中的 URL 获取查询字符串

    有没有办法在 Rails 中获取传递的 URL 字符串中的查询字符串 我想传递一个 URL 字符串 http www foo com id 4 empid 6 我怎样才能得到id and empid 如果字符串中有 URL 则使用 URI
  • 是否需要“严格使用”Python 编译器?

    存在Python静态分析工具 https stackoverflow com questions 35470 are there any static analysis tools for python 但编译时检查往往与运行时绑定哲学 h
  • Spark配置优先级

    在代码中指定 Spark 应用程序配置之间是否有任何区别或优先级 SparkConf setMaster yarn 并在命令行中指定它们 spark submit master yarn 是的 用户代码中使用 set 函数进行的配置具有最高
  • 如何使用 ReactiveCocoa 3 实现基本的 UITextField 输入 + UIButton 操作场景?

    我同时是 Swift 和 ReactiveCocoa 菜鸟 我想使用 MVVM 和 Reactive Cocoa v3 0 beta 4 框架来实现此设置 以学习新的 RAC 3 框架的基础知识 我有一个文本字段 我希望文本输入包含 3 个
  • Ubuntu 上托盘消息的 Java 外观

    我通过托盘Icon displayMessage 在系统托盘上显示消息 但它们看起来不太好 我尝试将它们的外观和感觉更改为系统标准 但在创建图标之前调用 UIManager setLookAndFeel 并没有改变任何内容 UIManage
  • Github 中的“blob”对应什么?

    下面 URL 中 blob 后面的单词指向给定存储库的 master 分支 https github com celery celery blob master docs django first steps with django rst
  • 如果 vue 组件上的模式关闭,如何重置下拉数据?

    我的情况是这样的 我有两个组件 父组件和子组件 我的父组件是这样的
  • 在 Python 中,避免 __init__ 参数和实例变量使用相同名称的最佳方法是什么?

    初始化实例变量的最佳方法是什么init功能 两次使用同一个名字是不是很糟糕 class Complex def init self real imag self real real self imag imag 在我看来 想出这样的任意替代
  • Android Intent 打开“海量存储活动”

    我需要从我的应用程序中打开 USB 海量存储活动 有没有Intent去做这个 就像是 startActivity new Intent Settings ACTION APPLICATION DEVELOPMENT SETTINGS 您可以
  • 解析 docker-compose 中 extra_hosts 选项中的容器名称

    我需要从另一个容器卷曲我的 API 容器 1 称为nginx容器 2 称为fpm 我需要能够猛击我的fpm容器并卷曲nginx容器 Config docker compose yaml services nginx build contex
  • 如何安装和运行 Firefox 的 puppeteer

    你好 我正在做一些网络自动化 我正在尝试打开一个 url 但在 chrome 控制台中收到数据 URL 错误 因此我正在转向 Firefox 控制台 以解决 chrome 控制台中没有打开数据 url 的问题 问题是 npm install
  • HOpenGL 对于 Haskell 中的其他线程和 TChan 有何行为?

    我正在为一个相当复杂的视频游戏做一些概念验证工作 我想使用 HOpenGL 库在 Haskell 中编写 我首先编写一个模块来实现基于客户端 服务器事件的通信 当我尝试将其连接到一个简单的程序以在屏幕上绘制点击时 我的问题出现了 事件库使用
  • 类型错误:预期的字符串或类似字节的对象(Python 3)(Wordcloud)

    import pandas as pd import matplotlib pyplot as plt from PIL import Image import numpy as np import wordcloud from wordc