我想将png文件保存到我的Mongodb数据库中。 我正在尝试使用以下代码:
with open(SCREENCAP_FILE_NAME, 'rb') as f:
page_info_tuple = {
"page_hash": _page_hash,
"ativity_name": _activity_name,
"screen_shot": f
}
Mongo.coll_pageinfo.insert_one(page_info_tuple)
但是,它给了我
bson.errors.InvalidDocument:无法编码对象:打开文件“ screen.png”,模式为“ rb”,位于0x109d3ced0
答案 0 :(得分:0)
我通过使用如下代码解决了这个问题:
from bson import Binary
with open(SCREENCAP_FILE_NAME, mode='rb') as f:
page_info_tuple = {
"page_hash": _page_hash,
"ativity_name": _activity_name,
"screen_shot": Binary(f.read())
}
Mongo.coll_pageinfo.insert_one(page_info_tuple)