如何从 couchdb 检索所有文档并使用 python 将其转换为 CSV

How to retrieve all docs from couchdb and convert it as CSV using python

我需要从 couchdb 中检索具有指定字段的所有文档,但只能从一个文档中获取结果。但是当我打印它时,所有文档都显示

for item in db.view('_all_docs'):
    doc = item.doc
    _id = item['id']

    f = open('retrieve.csv', 'w')
    writer = csv.writer(f)
    writer.writerow([_id])

要获取全部,打开的文件需要在循环外

f = open('retrieve.csv', 'w')
writer = csv.writer(f)
for item in db.view('_all_docs'):
    doc = item.doc
    _id = item['id']
    writer.writerow([_id])

如果您想将 CouchDB 数据集转换为 CSV,您可以使用 CouchDB 显示功能(如果您只需要对一个文档执行此操作)或使用 CouchDB 列表功能(如果您需要合并多个文档)。

http://docs.couchdb.org/en/2.1.2/ddocs/ddocs.html#show-functions

http://guide.couchdb.org/draft/show.html

http://guide.couchdb.org/draft/transforming.html