繁体   English   中英

使用列创建表会产生 TypeError: string operation on non-string array

[英]creating tables with columns yields TypeError: string operation on non-string array

我正在使用一个 jupyter notebook,程序在其中运行一本书,然后创建一个表格来绘制我从书中挑选的常用单词列表并相应地显示它们。 我绝对觉得我在桌子的某个地方搞砸了,因为它没有打印出来并给我下面提供的这个错误。 我还提供了执行我写出的表格的代码。 我是一名新手程序员,所以对于任何格式问题我深表歉意,谢谢。

看书的初始化和代码

from datascience.tables import Table
import numpy as np
%matplotlib inline
import matplotlib.pyplot as plots
plots.style.use('fivethirtyeight')
import warnings
warnings.simplefilter(action="ignore", category=FutureWarning)
from urllib.request import urlopen 
import re

def read_url(url): 
    return re.sub('\\s+', ' ', urlopen(url).read().decode())

def read_file(name):
    return re.sub('\\s+', ' ', open(name, "r").read())
book = read_url("https://www.gutenberg.org/files/8438/8438-0.txt")
chapters = book.split('CHAPTER ')[1:]

返回 TypeError 的表的代码

table = Table().with_columns([
    "Aristotle",   np.char.count(chapters, "Aristotle"),
    "mathematics",  np.char.count(chapters, "mathematics"),
    "science",  np.char.count(chapters, "science"),
    "morals",  np.char.count(chapters, "morals"),
    "virtues", np.char.count(chapters, "virtues"),
    "ethics", np.char.count(chapters, "ethics")
])
table
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
~\AppData\Local\Temp\ipykernel_3412\3432057997.py in <module>
      1 table = Table().with_columns([
----> 2     "Aristotle",   np.char.count(chapters, "Aristotle"),
      3     "mathematics",  np.char.count(chapters, "mathematics"),
      4     "science",  np.char.count(chapters, "science"),
      5     "morals",  np.char.count(chapters, "morals"),

<__array_function__ internals> in count(*args, **kwargs)

B:\anaconda\lib\site-packages\numpy\core\defchararray.py in count(a, sub, start, end)
    505 
    506     """
--> 507     return _vec_string(a, int_, 'count', [sub, start] + _clean_args(end))
    508 
    509 

TypeError: string operation on non-string array 

在完全不同的书上工作的相同代码的图像

我尝试基本上镜像这种格式,但它最终返回了一个 TypeError。 我仔细检查了 python 中的所有软件包是否正确安装,并希望我的代码也能运行,但是它出现了这个我似乎无法克服的错误。

count方法需要一个字符串数组作为它的第一个参数,但chapters是一个字符串。 要计算每章单词列表中每个单词的出现次数,您可以尝试使用列表理解。 这是一个例子:

counts = []
for chapter in chapters:
    count = [np.char.count(chapter, word) for word in ["Aristotle", "mathematics", "science", "morals", "virtues", "ethics"]]
    counts.append(count)

table = Table().with_columns([
    "Aristotle", [count[0] for count in counts],
    "mathematics", [count[1] for count in counts],
    "science", [count[2] for count in counts],
    "morals", [count[3] for count in counts],
    "virtues", [count[4] for count in counts],
    "ethics", [count[5] for count in counts]
])
table

更新

为了解析这本特定的书,您可以在 for 循环中自动生成以下模式:

pattern = """




BOOK I

Chapter I.


"""

如果代码性能对您来说无关紧要,只需替换罗马数字并将拆分方法应用于加载到 memory 的整本书内容。 请注意,这种方法极度未优化,您应该只将它用于需要运行一次或两次的代码。 例如:

chapters = [book.split(pattern)[0] for pattern in patterns]

其中 patterns 是像上面那样的字符串列表

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM