将图像加载到mongodb时出现错误信息

时间:2017-02-15 14:23:10

标签: python mongodb pymongo gridfs

代码:

import pymongo, gridfs
client = MongoClient("localhost", 27017)
db = client.gridfs_example
fs = gridfs.GridFS(db)
fs.list()
fs.put(open('C:\\Users\\Mihir\\Desktop\\Python with web interface\\crap\\Capture.PNG', 'r'), filename = 'images')

错误:

UnicodeDecodeError: 'charmap' codec can't decode byte 0x9c in position 19: character maps to <undefined>

我试着没有运气搜索网页。

1 个答案:

答案 0 :(得分:1)

由于您正在处理图片,请尝试打开他们的字节,使用“rb”而不是“r”

fs.put(open('C:\\Users\\Mihir\\Desktop\\Python with web interface\\crap\\Capture.PNG', 'rb'), filename = 'images')

Python正在尝试将图像字节转换为字符串