在Python中读取BSON文件? [英] Read BSON file in Python?
本文介绍了在Python中读取BSON文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想用Python读取BSON格式的Mongo转储并处理数据。我正在使用Python bson软件包(我更喜欢使用它而不是pymongo依赖项),
I want to read a BSON format Mongo dump in Python and process the data. I am using the Python bson package (which I'd prefer to use rather than have a pymongo dependency), but it doesn't explain how to read from a file.
这就是我要尝试的:
bson_file = open('statistics.bson', 'rb')
b = bson.loads(bson_file)
print b[0]
但是我得到:
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__'
我在做什么错了?
推荐答案
文档指出:
> help(bson.loads)
Given a BSON string, outputs a dict.
您需要传递字符串。例如:
You need to pass a string. For example:
> b = bson.loads(bson_file.read())
这篇关于在Python中读取BSON文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文