我有以下测试脚本:
# -*- 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
请求附件会返回以下响应 header :
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 附件的编码?
最佳答案
正如 couchdb 邮件列表中所建议的那样,解决方案是:
db.put_attachment(doc, content, content_type='text/plain;charset=utf-8')
关于python - 为 couchdb 指定附件编码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20905157/