[英]How to process the file being downloaded chunk by chunk using GridFSBucket
我的目標是編寫 python 腳本,它使用 gridfs 讀取文本文件。 並逐行迭代它。 當我使用gridfs.get()
,注意到我在每次迭代中都獲得了大量字節。請指導我如何使用“get”逐行迭代。
我可以通過使用GridFsBucket
並將不必要的數據存儲在臨時文件中,並在讀取模式下再次打開以逐行迭代來管理它。 尋找更好的方法來處理這個問題。
file_store = GridFSBucket(db)
file = open('test.txt', 'wb')
file_store.download_to_stream(raw_file[0].get('ObjectId'),file)
if not file:
return None
file.close()
file=open('test.txt','rb')
for line in file:
.....
能夠使用GridFSBucket
和open_download_stream
實現這open_download_stream
。
下面是示例代碼:
file_store = GridFSBucket(mongo.db, bucket_name=<fs CollectionName>)
file_handler = file_store.open_download_stream(object_id)
eachline=file_handler.readline()
while eachline:
.........processss
eachline = file_handler.readline()
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.