【问题标题】:Handling results of eXist-db query made with Python使用 Python 处理 eXist-db 查询的结果
【发布时间】:2013-08-14 12:22:18
【问题描述】:

我在 python 中完成了以下代码,以获取存储在 eXist-db 中的 XML 查询响应。我得到了值,但问题是下面的输出中给出的“即时”类型。这是我的代码:

from eulexistdb import db
class TestExist:
    def __init__(self):
        self.db = db.ExistDB("http://localhost:8899/exist/")   

    def get_res(self,query):
        #result = list()
        res = self.db.executeQuery(query)
        hits = self.db.getHits(res)        
        for i in range(hits):
            print self.db.retrieve(res,i)
            print type(self.db.retrieve(res,i))
xquery = '''
let $x:= doc("/db/sample/books.xml")
return $x/bookstore/book/price/text()'''
a = TestExist()
a.get_res(xquery)

现在查询工作正常,结果也打印为:

30.00
<type 'instance'>
29.99
<type 'instance'>
49.99
<type 'instance'>
39.95
<type 'instance'>

我想要的是返回列表“结果”中附加的值。我尝试了类型转换但失败了。我如何做到这一点?

【问题讨论】:

  • 你说,“我得到了价值,但问题是。”请编辑您的帖子以完成这句话。

标签: python xquery exist-db


【解决方案1】:

这很奇怪 - docs 似乎说 db.retrieve 函数应该返回一个字符串,但显然它没有。在任何情况下,由于 print 语句从中获取了有用的字符串,因此其中一个应该可以工作:

result.append(str(self.db.retrieve(res,i)))

result.append(repr(self.db.retrieve(res,i)))

只需取消注释 #result = list() 行,并将上述之一添加到 for 循环中。

【讨论】:

  • 转换为 str() 有效。太傻了,我没想到。我想从中浮动价值。但是 repr() 没有像 ['', '', '', '']
  • 太棒了——很高兴它成功了。如果您认为它解决了您的问题,点击答案旁边的“接受”复选标记将有助于其他用户知道它有效。
猜你喜欢
  • 1970-01-01
  • 2017-05-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-10-28
  • 2011-08-14
  • 1970-01-01
相关资源
最近更新 更多