Read a BSON file in Python?

I want to read the Mongo format in BSON format in Python and process the data. I use Python's bson package (which I would prefer to use rather than the pymongo dependency), but it does not explain how to read from a file.

Here is what I am trying:

bson_file = open('statistics.bson', 'rb') b = bson.loads(bson_file) print b[0] 

But I get:

 Traceback (most recent call last): File "test.py", line 11, in <module> b = bson.loads(bson_file) File "/Library/Python/2.7/site-packages/bson/__init__.py", line 75, in loads return decode_document(data, 0)[1] File "/Library/Python/2.7/site-packages/bson/codec.py", line 235, in decode_document length = struct.unpack("<i", data[base:base + 4])[0] TypeError: 'file' object has no attribute '__getitem__' 

What am I doing wrong?

+6
source share
3 answers

The documentation states:

 > help(bson.loads) Given a BSON string, outputs a dict. 

You need to pass the string. For instance:

 > b = bson.loads(bson_file.read()) 
+6
source

I found this worked for me using the monsodb 2.4 BSON file and the python 'bson' module:

 import bson with open('survey.bson','rb') as f: data = bson.decode_all(f.read()) 

This returned a list of dictionaries matching the JSON documents stored in this mongo collection.

The f.read () data looks like this in BSON:

 >>> rawdata[:100] '\x04\x01\x00\x00\x12_id\x00\x01\x00\x00\x00\x00\x00\x00\x00\x02_type\x00\x07\x00\x00\x00simple\x00\tchanged\x00\xd0\xbb\xb2\x9eI\x01\x00\x00\tcreated\x00\xd0L\xdcfI\x01\x00\x00\x02description\x00\x14\x00\x00\x00testing the bu' 
+3
source

loads expects a string (which means 's'), not a file. Try to read from the file and pass the result to loads .

+2
source

Source: https://habr.com/ru/post/979789/


All Articles