手作りのファイルはありませんが、手でデータを解析する方法があると思います。
source for bson.decode_file_iter
(サンセリフのdocstring)はこのように書きます:私は推測
_UNPACK_INT = struct.Struct("<i").unpack
def decode_file_iter(file_obj, codec_options=DEFAULT_CODEC_OPTIONS):
while True:
# Read size of next object.
size_data = file_obj.read(4)
if len(size_data) == 0:
break # Finished with file normaly.
elif len(size_data) != 4:
raise InvalidBSON("cut off in middle of objsize")
obj_size = _UNPACK_INT(size_data)[0] - 4
elements = size_data + file_obj.read(obj_size)
yield _bson_to_dict(elements, codec_options)
、時間のかかる操作が_bson_to_dict
呼び出しです - あなたは1を必要としません。
したがって、ファイルを読み取るだけで、次のドキュメントのサイズでint32値を取得してスキップするだけで済みます。次に、これを実行した文書の数を数えます。パッケージをBSON
import struct
import os
from bson.errors import InvalidBSON
def count_file_documents(file_obj):
"""Counts how many documents provided BSON file contains"""
cnt = 0
while True:
# Read size of next object.
size_data = file_obj.read(4)
if len(size_data) == 0:
break # Finished with file normaly.
elif len(size_data) != 4:
raise InvalidBSON("cut off in middle of objsize")
obj_size = struct.Struct("<i").unpack(size_data)[0] - 4
# Skip the next obj_size bytes
file_obj.seek(obj_size, os.SEEK_CUR)
cnt += 1
return cnt
(でも、私は、コードをテストしていない手でのMongoDBを持っていない。。)
されています
だから、私は信じて、この関数はトリックを行う必要がありますあなたは使っていますか?これはmongodb bsonか別のものですか? – 16num
はこの情報で質問を更新しました。私が使用しているbsonパッケージは、以下のhttps://api.mongodb.com/python/current/api/bson/index.html – user1438162