如何用Python合并lmdb文件
由于Caffe使用的存儲(chǔ)圖像的數(shù)據(jù)庫是lmdb,因此有時(shí)候需要對(duì)lmdb文件進(jìn)行操作,本文主要講解如何用Python合并lmdb文件。沒有l(wèi)mdb支持的,需要用pip命令安裝。
pip install lmdb
代碼及注釋如下:
# coding=utf-8
# filename: merge_lmdb.py
import lmdb
# 將兩個(gè)lmdb文件合并成一個(gè)新的lmdb
def merge_lmdb(lmdb1, lmdb2, result_lmdb):
print 'Merge start!'
# env代表Environment, txn代表Transaction
# 打開lmdb文件,讀模式
env_1 = lmdb.open(lmdb1)
env_2 = lmdb.open(lmdb2)
# 創(chuàng)建事務(wù)
txn_1 = env_1.begin()
txn_2 = env_2.begin()
# 打開數(shù)據(jù)庫
database_1 = txn_1.cursor()
database_2 = txn_2.cursor()
# 打開lmdb文件,寫模式,
env_3 = lmdb.open(result_lmdb, map_size=int(1e12))
txn_3 = env_3.begin(write=True)
count = 0
# 遍歷數(shù)據(jù)庫
for (key, value) in database_1:
# 將數(shù)據(jù)放到結(jié)果數(shù)據(jù)庫事務(wù)中
txn_3.put(key, value)
count++
if(count % 1000 == 0):
# 將數(shù)據(jù)寫入數(shù)據(jù)庫,必須的,否則數(shù)據(jù)不會(huì)寫入到數(shù)據(jù)庫中
txn_3.commit()
count = 0
txn_3 = env_3.begin(write=True)
if(count % 1000 != 0):
txn_3.commit()
count = 0
txn_3 = env_3.begin(write=True)
for (key, value) in database_2:
txn_3.put(key, value)
if(count % 1000 == 0):
txn_3.commit()
count = 0
txn_3 = env_3.begin(write=True)
if(count % 1000 != 0):
txn_3.commit()
count = 0
txn_3 = env_3.begin(write=True)
# 關(guān)閉lmdb
env_1.close()
env_2.close()
env_3.close()
print 'Merge success!'
# 輸出結(jié)果lmdb的狀態(tài)信息,可以看到數(shù)據(jù)是否合并成功
print env_3.stat()
def main():
fr = open('lmdb.txt')
# lmdb1的目錄
lmdb1 = fr.readline().strip()
# lmdb2的目錄
lmdb2 = fr.readline().strip()
# result lmdb的目錄
result_lmdb = fr.readline().strip()
fr.close()
merge_lmdb(lmdb1, lmdb2, result_lmdb)
if __name__ == '__main__':
main()
以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
flask post獲取前端請(qǐng)求參數(shù)的三種方式總結(jié)
這篇文章主要介紹了flask post獲取前端請(qǐng)求參數(shù)的三種方式總結(jié),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-12-12
Python錯(cuò)誤提示:[Errno 24] Too many open files的分析與解決
這篇文章主要給大家介紹了Python中出現(xiàn)錯(cuò)誤提示:[Errno 24] Too many open files的分析與解決,需要的朋友可以參考借鑒,下面來一起看看吧。2017-02-02
Python3實(shí)現(xiàn)的爬蟲爬取數(shù)據(jù)并存入mysql數(shù)據(jù)庫操作示例
這篇文章主要介紹了Python3實(shí)現(xiàn)的爬蟲爬取數(shù)據(jù)并存入mysql數(shù)據(jù)庫操作,涉及Python正則爬取數(shù)據(jù)及針對(duì)mysql數(shù)據(jù)庫的存儲(chǔ)操作相關(guān)實(shí)現(xiàn)技巧,需要的朋友可以參考下2018-06-06
python實(shí)現(xiàn)發(fā)送郵件及附件功能
這篇文章主要為大家詳細(xì)介紹了python實(shí)現(xiàn)發(fā)送郵件及附件功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-05-05

