0

我的mongodb使用utf-8,django从中读取数据不是unicode,也许是答案,但如何解决这个问题?

mongodb中的数据有一些中文。如果我完全使用字母或数字的另一个表,它会很好用。

类源(文档):
    id = IntField()
    url = 字符串字段()
    fetchTime = StringField()
    域 = 字符串字段()
    xml_tpl_old = StringField()
    xml_tpl_new = 字符串字段()
    源 = BinaryField()
    xmlResult = StringField()
    xmlResult1 = StringField()

如果我使用源表(它有一些中文数据)

Source.objects.all()

django 会给消息


Traceback (most recent call last):
  File "E:/Django_project/mysite/mysite/test.py", line 49, in 
    unicode(Source.objects)
  File "D:\Python27\lib\site-packages\mongoengine-0.8.0rc4-py2.7.egg\mongoengine\queryset\queryset.py", line 216, in __repr__
    self._populate_cache()
  File "D:\Python27\lib\site-packages\mongoengine-0.8.0rc4-py2.7.egg\mongoengine\queryset\queryset.py", line 167, in _populate_cache
    self._result_cache.append(self.next())
  File "D:\Python27\lib\site-packages\mongoengine-0.8.0rc4-py2.7.egg\mongoengine\queryset\queryset.py", line 1148, in next
    raw_doc = self._cursor.next()
  File "D:\Python27\lib\site-packages\pymongo-2.5-py2.7-win32.egg\pymongo\cursor.py", line 814, in next
  File "D:\Python27\lib\site-packages\pymongo-2.5-py2.7-win32.egg\pymongo\cursor.py", line 763, in _refresh
  File "D:\Python27\lib\site-packages\pymongo-2.5-py2.7-win32.egg\pymongo\cursor.py", line 720, in __send_message
  File "D:\Python27\lib\site-packages\pymongo-2.5-py2.7-win32.egg\pymongo\helpers.py", line 106, in _unpack_response
UnicodeDecodeError: 'utf8' codec can't decode byte 0xa1 in position 74: invalid start byte

谢谢!

4

2 回答 2

-1

在你的 python 控制台中试试这个

>>>import sys
>>>sys.getdefaultencoding()

如果结果是'ascii',你应该改变默认编码。

于 2013-05-14T02:33:32.180 回答
-2

在将数据保存到数据库之前,我使用 base64 编码。

于 2013-05-14T04:50:39.877 回答