我正在尝试读取tgz文件并将其写入沙发床。

这是代码。

import couchdb
conn = couchdb.Server('http://localhost:5984')
db = conn['test']

with open('/tmp/test.txt.tgz.enc') as f:
     data = f.read()
     doc = {'file': data}
     db.save(doc)


它失败了

Traceback (most recent call last):
  File "<stdin>", line 4, in <module>
  File "/usr/local/lib/python2.7/dist-packages/couchdb/client.py", line 407, in save
    _, _, data = func(body=doc, **options)
  File "/usr/local/lib/python2.7/dist-packages/couchdb/http.py", line 399, in post_json
    status, headers, data = self.post(*a, **k)
  File "/usr/local/lib/python2.7/dist-packages/couchdb/http.py", line 381, in post
    **params)
  File "/usr/local/lib/python2.7/dist-packages/couchdb/http.py", line 419, in _request
    credentials=self.credentials)
  File "/usr/local/lib/python2.7/dist-packages/couchdb/http.py", line 176, in request
    body = json.encode(body).encode('utf-8')
UnicodeDecodeError: 'ascii' codec can't decode byte 0xe3 in position 11: ordinal not in range(128)


仍在谷歌搜索自己找到解决方案。

最佳答案

好吧,我解决了。仔细检查了文档,并有一个put_attachment函数,但它要求您先创建一个文档,然后将附件分配给该文档。

代码示例,即使其他人需要它:

import couchdb

conn = couchdb.Server('http://localhost:5984')
db = conn['test1']

doc = {'name': 'testfile'}
db.save(doc)
db.put_attachment(doc, data, filename="test.txt.tgz")

关于python - 用Python将tgz存储到couchdb,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/13545119/

10-12 21:24