IndexError:列表索引超出范围(在查询结果中)

时间:2010-11-21 04:21:22

标签: python google-app-engine

我在理解如何使用查询结果时遇到问题。我问了六个关于这个的问题,但我还是不明白。我从以前的代码复制,我让它以某种方式工作,但由于我不理解底层的概念,如果我做一个小的改变代码崩溃。如果你能告诉我你如何看待这里发生的事情并向我解释,我真的很感激。谢谢。

class ReceiveEmail(InboundMailHandler):
    def receive(self, message):
        logging.info("Received email from %s" % message.sender)
        plaintext = message.bodies(content_type='text/plain')
        for text in plaintext:
            txtmsg = ""
            txtmsg = text[1].decode()
            logging.info("Body is %s" % txtmsg)
            logging.info("CC email is %s" % ((message.cc).split(",")[1]))            

        query = User.all()           
        query.filter("userEmail =",  ((message.cc).split(",")[1])) 
        results = query.fetch(1)                   

        for result in results:                     
            result.userScore += 1                  

        um = results[0]                            
        um.userScore = result.userScore            
        um.put()

在此代码中,据我所知,查询从cc列表中获取第二个电子邮件地址并获取结果。

然后我将userScore增加1.

接下来,我想更新数据存储区中的这个项目,所以我说

        um = results[0]                            
        um.userScore = result.userScore            
        um.put()

但这会导致索引超出范围错误:

um = results[0]
IndexError: list index out of range

为什么呢?我想象results[0]是结果的零项。为什么它超出范围?我唯一能想到的是,列表可能是None。但我不明白为什么。它必须有1个被提取的项目。

此外,如果我尝试通过将索引从[1]更改为[0]来测试第一个电子邮件地址

query.filter("userEmail =",  ((message.cc).split(",")[0]))

然后我没有得到IndexError

我在这里做错了什么?

谢谢!

修改

见评论:

(message.cc).split(",")[0]) 

在电子邮件前留下了一个空格(从第二封电子邮件开始),因此查询与它们不匹配;

>>> cc.split(",")
['cc12@example.com', ' cc13@example.com', ' cc13@example.com']

用逗号修改问题后添加空格:

>>> listcc = cc.split(", ")
>>> listcc
['cc12@example.com', 'cc13@example.com', 'cc13@example.com']
>>> 

1 个答案:

答案 0 :(得分:1)

要理解代码,请将其分解并逐一查看:

class ReceiveEmail(InboundMailHandler):
    def receive(self, message):
        logging.info("Received email from %s" % message.sender)

        # Get a list of CC addresses.  This is basically a for loop.
        cc_addresses = [address.strip() for address in message.cc.split(",")]
        # The CC list goes with the message, not the bodies.
        logging.info("CC email is %s" % (cc_addresses))

        # Get and iterate over all of the *plain-text* bodies in the email.
        plaintext = message.bodies(content_type='text/plain')
        for text in plaintext:
            txtmsg = ""
            txtmsg = text[1].decode()
            logging.info("Body is %s" % txtmsg)

        # Setup a query object.
        query = User.all()
        # Filter the user objects to get only the emails in the CC list.
        query.filter("userEmail IN",  cc_addresses)
        # But, only get at most 10 users.
        users = query.fetch(10)

        logging.info('Got %d user entities from the datastore.' % len(users))

        # Iterate over each of the users increasing their score by one.
        for user in users:
            user.userScore += 1

        # Now, write the users back to the datastore.
        db.put(users)
        logging.info('Wrote %d user entities.' % len(users))

我会调整您的模型结构。创建用户实体时,我会将key_name设置为电子邮件地址。您将能够提高查询效率。

一些参考文献: