【问题标题】:Querying the Datastore in python在 python 中查询数据存储
【发布时间】:2010-05-06 15:34:51
【问题描述】:

您好!

我正在尝试使用 datatstore 中的单个列,我可以查看和显示内容,如下所示:

q = test.all()
q.filter("adjclose =", "adjclose")
q = db.GqlQuery("SELECT * FROM test")

results = q.fetch(5)
for p in results:
    p1 = p.adjclose     
    print "The value is --> %f" % (p.adjclose)

但是我需要用 adjclose 列计算历史值,我无法克服错误

for c in range(len(p1)-1):
    TypeError: object of type 'float' has no len()

这是我的代码!

for c in range(len(p1)-1):
    p1.append(p1[c+1]-p1[c]/p1[c])
    p2 = (p1[c+1]-p1[c]/p1[c])
    print "the p1 value<--> %f" % (p2)
    print "dfd %f" %(p1)

python 新手,任何帮助将不胜感激!

提前致谢

这是完整的代码

class CalHandler(webapp.RequestHandler):

    def get(self):
        que = db.GqlQuery("SELECT * from test")
        user_list = que.fetch(limit=100)

        doRender(
            self,
            'memberscreen2.htm',
            {'user_list': user_list} )


q = test.all()
q.filter("adjclose =", "adjclose")
q = db.GqlQuery("SELECT * FROM test")

results = q.fetch(5)
for p in results:
    p1 = p.adjclose     
    print "The value is --> %f" % (p.adjclose)
    for c in range(len(p1)-1):
        p1.append(p1[c+1]-p1[c]/p1[c])
        print "the p1 value<--> %f" % (p2)
        print "dfd %f" %(p1)

【问题讨论】:

    标签: google-app-engine google-cloud-datastore


    【解决方案1】:

    我会这样做:

    for c in xrange(p1-1)
    

    本质上,听起来 p1 已经包含 len,所以你可以直接使用它。另外,请注意使用xrange 函数通常比range 快一点。原因是xrange 返回一个迭代器,而range 构建一个列表。换句话说,如果您执行range(1000000),它将创建一个包含一百万个整数的列表!

    【讨论】:

      【解决方案2】:

      错误告诉您绑定到p1 的值没有长度。这是有道理的,因为它是一个浮点数(也在错误消息中)。

      我怀疑您输入的是p 而不是p1。这是一个很好的例子,说明了为什么你不应该使用无意义的变量名。如果名称是 current_result(而不是 p)和 current_adjclose 而不是 p1,这将更容易发现。

      看起来您可能正在尝试在您的内部循环中进行某种转换(由于变量名称,它不是 100% 清楚)。如果是这样,那您需要使用list comprehension

      此外,您似乎正在尝试重用变量;不。毕竟,它们并不缺乏,而且很难选择有意义的变量名。

      【讨论】:

        猜你喜欢
        • 2015-08-31
        • 1970-01-01
        • 1970-01-01
        • 2013-06-21
        • 1970-01-01
        • 1970-01-01
        • 2022-11-15
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多