python - 为 couchdb 指定附件编码

标签 python unicode couchdb

我有以下测试脚本:

# -*- 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/

相关文章:

javascript - grunt js 缩小转换 unicode 字符

python - 匹配所有 utf-8/unicode 小写字母形式的正确正则表达式是什么

ios - CouchDB 中错误超时的原因

couchdb - OLAP 中的 CouchDB 怎么样?

python - 如何在已排序的日期列表中查找缺失的日期?

python - 无法在opencv python中编写视频

python - 在Scrapy中,无法提取其中包含 "@"的链接文本

unicode - 使用 US 布局在 Emacs 中快速输入 unicode

java - ACRA 发送自定义数据 REPORTFIELD

python - 使用 cx_Freeze 构建 msi : ValueError: FCI error 1