Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/webpack/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 索引器:列表索引超出范围(在查询结果中)_Python_Google App Engine - Fatal编程技术网

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

Python 索引器:列表索引超出范围(在查询结果中),python,google-app-engine,Python,Google App Engine,我在理解如何处理查询结果时遇到问题。我问了六个问题,但我还是不明白。我从以前的代码中复制,并以某种方式使其工作,但由于我不理解基本概念,如果我做一个小的更改,代码就会崩溃。如果你能告诉我你是如何想象这里发生的事情并向我解释的,我将不胜感激。多谢各位 class ReceiveEmail(InboundMailHandler): def receive(self, message): logging.info("Received email from %s" % messa

我在理解如何处理查询结果时遇到问题。我问了六个问题,但我还是不明白。我从以前的代码中复制,并以某种方式使其工作,但由于我不理解基本概念,如果我做一个小的更改,代码就会崩溃。如果你能告诉我你是如何想象这里发生的事情并向我解释的,我将不胜感激。多谢各位

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
为什么??我想象
结果[0]
是结果的第0项。为什么超出范围?我唯一能想到的是,列表可能是
None
。但我不明白为什么。它必须具有已提取的1项

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

query.filter("userEmail =",  ((message.cc).split(",")[0]))
那么我就不会得到
索引器

我做错了什么

谢谢

编辑

见评论:

(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']
>>> 

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

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))
我会调整你的模型结构。创建用户实体时,我会将密钥名称设置为电子邮件地址。您将能够使查询更加高效

一些参考资料:


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

      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))
      
      我会调整你的模型结构。创建用户实体时,我会将密钥名称设置为电子邮件地址。您将能够使查询更加高效

      一些参考资料:


      首先,您有多确定是否有任何结果可获取?是的,问得好。我正在从Dev控制台发送一封包含
      user11@example.com
      cc777@example.com
      。我确保这两封电子邮件已经在数据库中,以便查询可以获取它们。但我刚刚再试了一次,索引0和1现在都不起作用。你能建议一些方法来检查查询看到了什么吗?谢谢。@Ignacio Vazquez Abrams:也许你是对的;查询似乎失败:if results:for result in results:result.userScore+=1 um=results[0]um.userScore=result.userScore um.put()其他:logging.info(“查询失败”)@Ignacio Vazquez Abrams:谢谢你的提示
      ((message.cc).split(“,”[0])
      在电子邮件前面留了一个空格,因此查询与它们不匹配;因此,在逗号后添加一个空格解决了这个问题:
      ((message.cc).split(“,”[0])
      您有多大把握首先要获取任何结果?是的,问得好。我正在从Dev控制台发送一封包含
      user11@example.com
      cc777@example.com
      。我确保这两封电子邮件已经在数据库中,以便查询可以获取它们。但我刚刚再试了一次,索引0和1现在都不起作用。你能建议一些方法来检查查询看到了什么吗?谢谢。@Ignacio Vazquez Abrams:也许你是对的;查询似乎失败:if results:for result in results:result.userScore+=1 um=results[0]um.userScore=result.userScore um.put()其他:logging.info(“查询失败”)@Ignacio Vazquez Abrams:谢谢你的提示
      ((message.cc).split(“,”[0])
      在电子邮件前面留了一个空格,因此查询与它们不匹配;因此,在逗号后添加一个空格修复了这个问题:
      ((message.cc).split(“,”[0])
      你好罗伯特:感谢您的详细解释。这很有帮助。我研究了你提到的三个链接。特别感谢您修改和组织代码;现在它更有意义了。在
      put()
      的情况下,我知道我们通过从
      userEmail
      列过滤
      cc\u地址,从表
      User
      中获取了10行。然后我们更新了
      userScore
      ,并通过
      db.put(users)
      将更新的行写回数据存储。查看开发控制台的数据存储查看器;我明白了,app engine只知道更新
      userScore
      。它实际上更新了实体上的所有属性,但因为我们只更改了
      userScore
      ,所以它是唯一更改的属性。你好,罗伯特:感谢您的详细解释。这很有帮助。我研究了你提到的三个链接。特别感谢您修改和组织代码;现在它更有意义了。在
      put()
      的情况下,我知道我们通过从
      userEmail
      列过滤
      cc\u地址,从表
      User
      中获取了10行。然后我们更新了
      userScore
      ,并通过
      db.put(users)
      将更新的行写回数据存储。查看开发控制台的数据存储查看器;我明白了,appengine只知道更新
      userScore
      。它实际上更新了实体上的所有属性,但因为我们只更改了
      userScore
      ,所以它是唯一更改的属性。