TypeError:非序列上的迭代

2024-05-15 22:07:43 发布

您现在位置:Python中文网/ 问答频道 /正文

我在这里浏览了很多关于这个的帖子,也搜索了一下。我使用过其他语言,但仍在学习Python,而且我对类也不太熟悉,所以我想我是因为我不了解类的行为方式。

我想开发一个epub阅读应用程序,发现roberto alasina的项目名为integrate,这是一个很好的起点,但它没有收集元数据,而且处理lib的epub也没有提取到足够的数据来完成我想做的其他一些事情。所以我找到了一个名为dustjacket的epub阅读班。

我需要进一步提取的原因是,我需要处理从应用程序的多个部分读取的数据,而不仅仅是读卡器本身。我计划在其中添加whoosh以启用搜索和索引,我将需要阅读章节,然后索引文件。我可以在书的导入过程中做到这一点,而实际的索引部分并不需要一个gui。

无论如何,在dist jacket中,我添加了日志记录,我可以看到我的方法正确地从epub中获取了目录:

    def __parse_oebps(self, epub):
    '''Construct the chapter list assuming that the ePub has files in OEBPS/.'''

    # Parse the chapters
    npoints = self.__toc.findall("//{%(tocns)s}navPoint" % {'tocns': self.__namespaces['ncx']})
    for p in npoints:                   
        #rt = p.getroottree()
        #title = p.findtext("{%(tocns)s}text" % {'tocns': self.__namespaces['ncx']}) # Label text           
        title = p.find(
            '{http://www.daisy.org/z3986/2005/ncx/}navLabel').find(
                '{http://www.daisy.org/z3986/2005/ncx/}text').text
        contentfile = p.find("{%(tocns)s}content[@src]" % {'tocns':self.__namespaces['ncx']}).attrib['src'] # Contentfile name
        #if self.__has_oebps:
        #   #contentfile = "OEBPS/" + contentfile
        #   contentfile = "OEBPS/" + contentfile
        #   log.info("content file: %s", contentfile)

        #return contentfile 
        #self.chapters.append(EpubChapter(epub, p.attrib['id'], p.attrib['playOrder'], contentfile, title))
        if title and contentfile:
            log.debug("Title:        %s", title)
            log.debug("content file: %s", contentfile)
            self.chapters.append([title, contentfile])

    return self.chapters

我输入了调试日志,可以很容易地看到实例是从类中正确调用的,并且title和contentfile在那里。它来自ebubtoc类,并从epub类中的此方法调用:

    def __read_toc(self):
    '''Construct the table of contents for this epub'''
    self.__toc = EpubToc(self)
    for l, c in self.__toc:
        log.debug("Title:        %s", l)
        log.debug("content file: %s", c)

现在,当此方法获取数据时,即当我获取此错误时:

    for l, c in self.__toc:
TypeError: iteration over non-sequence

在这一点上我不知道我做错了什么,为什么这不起作用。如果这还不够的话,我可以发布我正在使用的其他类。

谢谢


Tags: thetextindebugselflogfortitle
1条回答
网友
1楼 · 发布于 2024-05-15 22:07:43

如你所示,你的问题是这样的:

for l, c in self.__toc:

但是赛尔夫被宣布为:

self.__toc = EpubToc(self)

归根结底,您需要遍历列表,而不是对象。类似这样的东西应该可以工作,您可能需要进行一些调整以使它能够满足您的特定需要(例如,可能需要zip函数)

for c in self.__toc.get_chapter_titles():

相关问题 更多 >