Python 如何检查(计数)返回的数据?

Python 如何检查(计数)返回的数据?,python,peewee,flask-peewee,Python,Peewee,Flask Peewee,我在Peewee中得到了这个查询: return c = Products.get(Products.sku == article).get() 如何检查它是否返回数据 我试过: if c.count() > 0: if len(c) > 0 这对我不起作用 这是完整的代码: try: r = self.isProductExist(row['sku']) ## if (r.count() == 0): except peewee.DoesNotExist:

我在Peewee中得到了这个查询:

return c = Products.get(Products.sku == article).get()
如何检查它是否返回数据

我试过:

if c.count() > 0:

if len(c) > 0
这对我不起作用

这是完整的代码:

try:
    r = self.isProductExist(row['sku'])

    ## if (r.count() == 0):

except peewee.DoesNotExist:

   # Insert product


    def isProductExist(self, article):
      return Products.get(Products.sku == article).get()
你可以使用count

以下是工作代码:

进口皮维 从peewee进口* db=SqliteDatabase'/tmp/a.db' 类别ProductsModel: sku=CharField 类元: 数据库=db db.connect db.drop\u tablemodels=[Products],safe=True db.创建_表[产品] count=Products.select.count printcount=>0 如果不计算: Products.createsku=abc printProducts.select.count=>1 printpeewee.\uuuu版本\uuuuu=>3.0.18
你的代码有各种各样的错误

首先,此处不需要第二次呼叫获取:

return c = Products.get(Products.sku == article).get()
其次,您返回的作业毫无意义。改为:

 return Products.get(Products.sku == article)
如果产品存在,它将被退回。否则,将引发DoesNotExist异常,从而无需在任何地方调用count

要使代码在找不到产品的情况下仍能工作,请执行以下操作:

try:
    return Products.get(Products.sku == article)
except Products.DoesNotExist:
    # Product was not found.
    return

return c=Products.getProducts.sku==article.get不是有效代码。只需执行c=Products.getProducts.sku==article.get。你能详细说明你是如何知道它不起作用的吗?它会给你们一个错误吗?我把这个查询放在try:except peewee中code@Gundama对不起,我不理解你的问题。如果您提供更多的代码,我相信这里会有人提供帮助。完成,请参阅questioncount已经存在很长时间了,它不是针对3.x的。@coleifer谢谢,修复了。你看到其他错误了吗?没有,看起来不错。