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

时间:2017-12-06 02:17:47

标签: python cloudant

我一直在尝试从Cloudant中的文档中获取附件图像数据。 选择文档后我可以成功完成(使用_id直接提取等)。

现在尝试与使用选择器的“查询”操作结合使用,我遇到了麻烦。

这是我的代码。

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文档中找到任何用于将dict转换为文档对象的API ... [python-cloudant document]:http://python-cloudant.readthedocs.io/en/latest/index.html

任何建议都将受到高度赞赏。

1 个答案:

答案 0 :(得分:0)

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