简体   繁体   English

TypeError:非序列迭代

[英]TypeError: iteration over non-sequence

I have looked over quite a few posts on this here as well as googling for it. 我在这里浏览了很多关于此的文章,并进行了谷歌搜索。 I have used other languages but still learning Python and I'm also not as familiar with classes so I think I'm down to something I just don't understand about the way classes act. 我曾经使用过其他语言,但仍在学习Python,而且我也不熟悉类,因此我认为我只是对类的行为不了解。

I wanted to work on an epub reading app and found roberto alasina's project called integrate and it was a good starting point but it didn't collect metadata and also the epub handling libs are not extracted enough for some of the other things I want to do. 我想开发一个epub阅读应用程序,发现了roberto alasina的名为Integrated的项目,这是一个很好的起点,但是它没有收集元数据,并且epub处理lib的提取不足以完成我想做的其他一些事情。 So I found dustjacket which is an epub reading class. 因此,我发现了一个epub阅读班的防尘套。

The reason I need to extract this further is I need to handle reads of data from multiple parts of the app and not just the reader itself. 我需要进一步提取此信息的原因是,我需要处理从应用程序的多个部分读取的数据,而不仅仅是读取器本身。 I plan on adding whoosh to it to enable searching and for the indexing I will need to read the chapters and then index the files. 我计划在其中添加飞快移动以启用搜索功能,并且需要为索引建立索引,我需要阅读这些章节,然后为文件建立索引。 I can do this during the import of the book and don't really need a gui for the actual indexing part. 我可以在导入本书的过程中执行此操作,并且对于实际的索引编制部分确实不需要GUI。

Anyway in dist jacket I have added logging and I can see that my method is correctly grabbing the table of contents from the epub: 无论如何,我在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

I put in the debug logging and can easily see that the instance is properly called from the class and that the title and contentfile is there. 我放入调试日志记录,可以很容易地看到从类正确调用了实例,并且标题和内容文件在那里。 this is from the ebubtoc class and is called from this method in the epub class: 这来自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)

now when this method gets that data is when I get this error: 现在,当此方法获取数据时,就是我得到此错误的时间:

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

I have no idea at this point what I'm doing wrong and why this is not working. 我现在不知道我在做什么错,为什么这不起作用。 I can post the rest of the classes I'm using if this is not enough. 如果这还不够的话,我可以发布其余的课程。

Thanks 谢谢

Your issue, as you show, is this line: 如您所显示,您的问题是此行:

for l, c in self.__toc:

But self.__toc was declared to be: 但是self .__ toc被声明为:

self.__toc = EpubToc(self)

Bottom line is, you need to iterate over a list, not an object. 最重要的是,您需要遍历列表,而不是对象。 Something like this should work, you might need a bit of tweaking to get it to work for your specific needs (possible the zip function, for instance) 像这样的事情应该起作用,您可能需要进行一些调整才能使其适合您的特定需求(例如zip功能)

for c in self.__toc.get_chapter_titles():

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

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