0

我在这里查看了很多关于此的帖子,并在谷歌上搜索它。我使用过其他语言,但仍在学习 Python,而且我对类也不熟悉,所以我认为我只是不了解类的行为方式。

我想开发一个 epub 阅读应用程序,发现 roberto alasina 的项目称为集成,这是一个很好的起点,但它没有收集元数据,而且 epub 处理库的提取不足以用于我想做的其他一些事情. 所以我找到了防尘套,它是一个 epub 阅读课程。

我需要进一步提取它的原因是我需要处理来自应用程序多个部分的数据读取,而不仅仅是读取器本身。我计划向它添加 whoosh 以启用搜索和索引,我需要阅读章节然后索引文件。我可以在导入本书的过程中执行此操作,并且实际上并不需要用于实际索引部分的 gui。

无论如何,在 dist 夹克中我添加了日志记录,我可以看到我的方法正确地从 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

我输入了调试日志,可以很容易地看到该实例是从类中正确调用的,并且标题和内容文件在那里。这是来自 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

目前我不知道我做错了什么以及为什么这不起作用。如果这还不够,我可以发布我正在使用的其余课程。

谢谢

4

1 回答 1

0

正如您所展示的,您的问题是这一行:

for l, c in self.__toc:

但是 self.__toc 被宣布为:

self.__toc = EpubToc(self)

底线是,您需要遍历列表,而不是对象。像这样的东西应该可以工作,您可能需要进行一些调整才能使其满足您的特定需求(例如,可能是 zip 功能)

for c in self.__toc.get_chapter_titles():
于 2013-01-29T14:28:00.723 回答