I am trying to read a tgz file and writing it to couchdb.

here is the code.

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)

it fails with

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)

still googling around to find a solution myself.

有帮助吗?

解决方案

alright I solved it. double checked the documentation and there is a put_attachment function but it requires a document to be created upfront you will assign the attachment to.

code example just if somebody else needs it:

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")

其他提示

k i got it.See this below example db=couch.create('test1')-This is to create a database name with test1.doc={'name':'testfile'} -This is key value pair.f=open('/home/yamunapriya/pythonpractices/addd.py','r')-This is to open the file with read mode.db.save(doc)-to save the file couchdb.db.put_attachment(doc,f,filename="/home/yamunapriya/pythonpractices/addd.py") -in this the parameter doc-key value pair,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") 
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top