如何使用 get_attachment 调用 QueryResult (python cloudant)?

How to use get_attachment call upon QueryResult (python cloudant) ?

我一直在尝试从 Cloudant 中的文档中获取附件图像数据。 一旦选择了一个文档,我就可以成功地做到这一点(直接提取 _id 等)。

现在想结合使用选择器的"query"操作,我运行遇到麻烦了。

这是我的代码。

targetName="chibika33"
targetfile="chibitest.png"

#--------------------------------------------------
# get all the documents with the specific nameField 
#--------------------------------------------------
myDatabase.create_query_index(fields = ['nameField'])
selector = {'nameField': {'$eq': targetName}}
docs = myDatabase.get_query_result(selector)

#--------------------------------------------------
# get the attachment files to them, save it locally
#--------------------------------------------------
count = 0
for doc in docs:
    count=count+1
    result_filename="result%03d.png"%(count)

    dataContent = doc.get_attachment(targetfile, attachment_type='binary')  
    dataContentb =base64.b64decode(dataContent)    
    with open(result_filename,'wb') as output:
        output.write(dataContentb)

导致错误为;

Traceback (most recent call last):
  File "view8.py", line 44, in <module>

   dataContent = doc.get_attachment(targetfile, attachment_type='binary')
AttributeError: 'dict' object has no attribute 'get_attachment'

到目前为止,我一直无法在 python-cloudant-document...[python-cloudant 文档中找到任何用于将 dict 转换为文档对象的 API ]: http://python-cloudant.readthedocs.io/en/latest/index.html

任何建议将不胜感激。

get_query_result(...) 返回的结构不是文档数组。

尝试:

resp = myDatabase.get_query_result(selector)
for doc in resp['docs']:
    # your code here

查看文档:

http://python-cloudant.readthedocs.io/en/latest/database.html#cloudant.database.CloudantDatabase.get_query_result