我有以下测试脚本:
# -*- coding: utf-8 -*-
import os
import couchdb
GREEK = u'ΑΒΓΔ ΕΖΗΘ ΙΚΛΜ ΝΞΟΠ ΡΣΤΥ ΦΧΨΩ αβγδ εζηθ ικλμ νξοπ ρςτυ φχψω'
# Prepare a unicode file, encoded using ENCODING
ENCODING = 'utf-8'
filename = '/tmp/test'
open(filename, 'w').write(GREEK.encode(ENCODING))
# Create an empty document
server = couchdb.Server()
db = server['cdb-tests']
doc_id = 'testing'
doc = { }
db[doc_id] = doc
# Attach the file to the document
content = open(filename, 'rb') # Open the file for reading
db.put_attachment(doc, content, content_type='text/plain')
如您所见,该文件是utf-8编码的,但是当我将该文件附加到couchdb时,我无法指定此编码。因此,在http://localhost:5984/cdb-tests/testing/test
请求附件会返回以下响应标头:
HTTP/1.1 200 OK
Server: CouchDB/1.2.0 (Erlang OTP/R15B01)
ETag: "7y85tiUeF/UX9kqpKAzQEw=="
Date: Fri, 03 Jan 2014 13:43:36 GMT
Content-Type: text/plain
Content-MD5: 7y85tiUeF/UX9kqpKAzQEw==
Content-Length: 102
Content-Encoding: gzip
Cache-Control: must-revalidate
Accept-Ranges: none
使用浏览器查看附件显示完全乱码。如何存储couchdb附件的编码?
答案 0 :(得分:4)
正如couchdb邮件列表中所建议的那样,解决方案是:
db.put_attachment(doc, content, content_type='text/plain;charset=utf-8')