使用python将tgz存储到couchdb

时间:2012-11-24 20:09:31

标签: python python-2.7 couchdb couchdb-python

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

这是代码。

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)

仍在谷歌上搜寻自己的解决方案。

2 个答案:

答案 0 :(得分:3)

好吧我解决了。双重检查文档,并有一个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")

答案 1 :(得分:0)

ki得到了它。参见下面的例子db = couch.create('test1') - 这是用test1.doc = {'name':'testfile'}创建一个数据库名称 - 这是键值对。 f = open('/ home / yamunapriya / pythonpractices / addd.py','r') - 这是用read mode.db.save(doc)打开文件 - 保存文件couchdb.db.put_attachment(doc ,f,filename =“/ home / yamunapriya / pythonpractices / addd.py”) - 这个参数doc-key值对,f-filename / path with read / write mode,filename

import couchdb  

couch=couchdb.Server()
db=couch.create('test1') 
doc={'name':'testfile'}  
f=open('/home/yamunapriya/pythonpractices/addd.py','r')
db.save(doc)  
db.put_attachment(doc,f,filename="/home/yamunapriya/pythonpractices/addd.py")